I have a birthday-car which shows nicely all the upcoming birthdays in days. But there is no integration of sensors which gives me the first birthday coming up so I can make some automation or raise a flag to notify me.
So I went ahead and got it done in Node Red (I may move it to a template once I learn more about the syntax in HA vs. what I am used to in Node Red).
I am now able to identify which of the whole list is the birthday coming up first. This is the code for the Node Red function:
//Get length of the array
var arrayLength = msg.payload.length;
//Save the array
var birthdayList = msg.payload;
//Get the first birthday
var firstBirthdate;
//Loop variable
var i;
//Loop through and get the minimum days
if (Array.isArray(birthdayList)) {
//Save the first date for comparison
firstBirthdate = birthdayList[0];
//Loop through the rest
for (i = 1; i < arrayLength; i++) {
//node.warn(birthdayList[i].state + " < " + firstBirthdate.state);
if (Number(birthdayList[i].state) < Number(firstBirthdate.state)) {
firstBirthdate = birthdayList[i];
//node.warn("current is " + birthdayList[i].state);
}
else{
//node.warn("nothing for " + birthdayList[i].state);
}
}
}
msg.payload = firstBirthdate;
return msg;
This is my flow:
[{"id":"3717198c.c5c156","type":"inject","z":"e5872747.054fa8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"8","topic":"","payload":"","payloadType":"date","x":140,"y":2240,"wires":[["291c72a9.75590e"]]},{"id":"976b7b7a.5cf778","type":"comment","z":"e5872747.054fa8","name":"Birthday Check","info":"","x":140,"y":2200,"wires":[]},{"id":"291c72a9.75590e","type":"ha-get-entities","z":"e5872747.054fa8","name":"Get entities","server":"de97c73a.70e098","version":0,"rules":[{"property":"entity_id","logic":"starts_with","value":"sensor.birthday","valueType":"str"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":310,"y":2240,"wires":[["c213f0e5.6f59b"]]},{"id":"c213f0e5.6f59b","type":"function","z":"e5872747.054fa8","name":"First Birthday","func":"//Get length of the array\nvar arrayLength = msg.payload.length;\n//Save the array\nvar birthdayList = msg.payload;\n//Get the first birthday\nvar firstBirthdate;\n//Loop variable\nvar i;\n\n//Loop through and get the minimum days\nif (Array.isArray(birthdayList)) {\n //Save the first date for comparison\n firstBirthdate = birthdayList[0];\n //Loop through the rest\n for (i = 1; i < arrayLength; i++) {\n //node.warn(birthdayList[i].state + \" < \" + firstBirthdate.state);\n if (Number(birthdayList[i].state) < Number(firstBirthdate.state)) {\n firstBirthdate = birthdayList[i];\n //node.warn(\"current is \" + birthdayList[i].state);\n }\n else{\n //node.warn(\"nothing for \" + birthdayList[i].state);\n }\n }\n}\nmsg.payload = firstBirthdate;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":2240,"wires":[["e2bd79076e139beb"]]},{"id":"08a6eece79b9278e","type":"debug","z":"e5872747.054fa8","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":920,"y":2240,"wires":[]},{"id":"e2bd79076e139beb","type":"ha-sensor","z":"e5872747.054fa8","name":"first_birthday","entityConfig":"9feb0c5a587d6d03","version":0,"state":"payload","stateType":"msg","attributes":[],"inputOverride":"merge","outputProperties":[],"x":730,"y":2240,"wires":[["08a6eece79b9278e"]]},{"id":"de97c73a.70e098","type":"server","name":"Home Assistant","addon":true},{"id":"9feb0c5a587d6d03","type":"ha-entity-config","server":"de97c73a.70e098","deviceConfig":"","name":"first_birthday","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":""},{"property":"icon","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false}]
I am not able to figure out how to create a sensor in HA called sensor.first_birthday
I am able to get a sensor created called sensor.nodered…followed by a random number.
It only has the following attributes:
It has the state at 9 which is correct, and the icon, which is also correct, the date, weeks remaining. But not the rest. I need at least the friendly_name to know whose birthday is coming up and display it on my dashboard.
So some of the attributes are being picked up. No matter what I try on the Node Red side, adding attributes for entity.id, friendly_name, etc. Nothing changes on the HA side.
In reading about it, there is a config node which adds the confusion. Not a lot of clear documentation about what do do in there or how to set it up. btw, do I need one of these config nodes for each entity I am creating in Node Red?
Here is mine:
Does anyone have a clear example on how to create a sensor in HA using a payload in Node Red?
This is my payload:
Any guides out there I can read to get this done right? Or anyone done anything like this and can give me some pointers?