jcgoette / baby_buddy_homeassistant

This custom integration provides sensors for Baby Buddy API endpoints.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feedings total amount

mrmultifunk opened this issue · comments

Hello,

Thank you for this great integration!

Is it possible to extract the total feeding amount into a sensor directly from the Babybuddy API?

Do you mean the total amount per day? 24 hour period? Something else?

I should have been more precise. I was thinking the total amount in a 24-hour period.

Have you looked at the Statistics platform? Specifically, the total attribute?

Thanks for getting back to this. Yes, i am using the statistics platform now to get the totals. The challenge with this solution, is if changes are made in the Baby Buddy database, eg. if i enter a feeding in the past. I generally try to avoid this, but sometimes it is necessary.

As far as i can see, it does not look like the totals are a part of the Baby Buddy API, so maybe it is better to create a request in the Baby Buddy repo?

Gotcha, makes sense.

We could query API with specific lookback. Docs say date, so we just need to check if it would accept a datetime.

That would be great! I guess this would be the same with other totals (diapers, sleep etc.?)

I made some progress on this. If i call the feedings endpoint, it returns all the feedings recorded. I'm then using node red to filter this and create a sensor for the total amount and the number of bottles consumed.

Here is the flow:
[{"id":"7be2b9f2d4976dbd","type":"http request","z":"35c0e2ebaea0e359","name":"","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1410,"y":180,"wires":[["c06039fdbb1ffe0d"]]},{"id":"4e1076f69e2f3ead","type":"function","z":"35c0e2ebaea0e359","name":"API call","func":"var today = new Date();\nvar date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();\nvar url = \"http://192.168.0.242:8800/api/feedings/?child=2&end_min=\"+date;\n\nmsg.headers = {}\nmsg.method = \"GET\"\nmsg.url = url \nmsg.headers['Authorization'] = 'Token replace_with_your_token';\n\nnode.status({ fill: \"green\", shape: \"dot\", text: \"Date for API call: \"+date });\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1240,"y":180,"wires":[["7be2b9f2d4976dbd"]]},{"id":"c06039fdbb1ffe0d","type":"function","z":"35c0e2ebaea0e359","name":"Create sum from returned array","func":"var flasker = msg.payload.count;\nconst arr = msg.payload.results;\n\nconst sum = arr.reduce((accumulator, object) => {\n return accumulator + object.amount;\n}, 0);\n\nconsole.log(sum); \n\nmsg.total = sum;\nmsg.flasker = flasker;\nnode.status({ fill: \"green\", shape: \"dot\", text: \"Amount: \"+sum+\" mL\"+\", in \"+flasker+\" bottles.\" });\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1630,"y":180,"wires":[["64c86aadc96ce1f1","39d7d19af7f0a062"]]},{"id":"fb277876f28efae3","type":"ha-entity","z":"35c0e2ebaea0e359","name":"","server":"xxxxxxxxxx","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"marie_mme_total"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:baby-bottle"},{"property":"unit_of_measurement","value":"mL"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"total","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":2010,"y":180,"wires":[[]]},{"id":"64c86aadc96ce1f1","type":"rbe","z":"35c0e2ebaea0e359","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"total","topi":"topic","x":1850,"y":180,"wires":[["fb277876f28efae3"]]},{"id":"b9d1d93a5614f4cd","type":"ha-entity","z":"35c0e2ebaea0e359","name":"","server":"xxxxxxxxxx","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"marie_mme_antal_flasker"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:baby-bottle"},{"property":"unit_of_measurement","value":"flasker"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"flasker","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":2010,"y":240,"wires":[[]]},{"id":"39d7d19af7f0a062","type":"rbe","z":"35c0e2ebaea0e359","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"flasker","topi":"topic","x":1850,"y":240,"wires":[["b9d1d93a5614f4cd"]]}]

image