Check if an entity was a certain state in the last 24 hours

Use the get-history to get a list of state changes for the last 24 hours. Filter them using a switch node looking for a given state and then count the remaining to see if the entity was in that state during the time frame.

screenshot

[{"id":"3f683250.b26f1e","type":"inject","z":"ffbd7f06.4a014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":236,"y":1008,"wires":[["e60a02e6.d2afc"]]},{"id":"aa3e26cd.152828","type":"debug","z":"ffbd7f06.4a014","name":"do something","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":976,"y":1008,"wires":[]},{"id":"e60a02e6.d2afc","type":"api-get-history","z":"ffbd7f06.4a014","name":"","startdate":"","enddate":"","entityid":"light.kitchen_light","entityidtype":"is","useRelativeTime":true,"relativeTime":"24 hours","flatten":true,"output_type":"split","output_location_type":"msg","output_location":"payload","x":380,"y":1008,"wires":[["b162effa.fc78f"]]},{"id":"b162effa.fc78f","type":"switch","z":"ffbd7f06.4a014","name":"on?","property":"payload.state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":530,"y":1008,"wires":[["e4b98b67.bde238"]]},{"id":"e4b98b67.bde238","type":"join","z":"ffbd7f06.4a014","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":658,"y":1008,"wires":[["fff04d54.5615e"]]},{"id":"fff04d54.5615e","type":"switch","z":"ffbd7f06.4a014","name":"count > 0","property":"payload.length","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":true,"outputs":1,"x":796,"y":1008,"wires":[["aa3e26cd.152828"]]}]

Here's the same example but using a function node.

screenshot

[{"id":"f79cd7b0.604f98","type":"inject","z":"ffbd7f06.4a014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":332,"y":1200,"wires":[["753a1de3.091fa4"]]},{"id":"753a1de3.091fa4","type":"api-get-history","z":"ffbd7f06.4a014","name":"","startdate":"","enddate":"","entityid":"light.kitchen_light","entityidtype":"is","useRelativeTime":true,"relativeTime":"24 hours","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":476,"y":1200,"wires":[["389b5575.43217a"]]},{"id":"389b5575.43217a","type":"function","z":"ffbd7f06.4a014","name":"check state","func":"const state = \"on\";\nconst filtered = msg.payload.filter((entity) => entity.state === state);\n\nif(filtered.length > 0) {\n    msg.payload = filtered;\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":644,"y":1200,"wires":[["6a582450.883bdc"]]},{"id":"6a582450.883bdc","type":"debug","z":"ffbd7f06.4a014","name":"do something","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":832,"y":1200,"wires":[]}]