Poll state can't compare string properly

I have a simple poll state node which is meant to query my phone’s battery status every minute. When I have the State Type set to String, and attach it to a debug node, it outputs ‘charging’. Ok neat, so I want now a boolean to see whether it’s charging or not… so I change the State Type to Boolean and in the If State row, I have the checker as is and the string to check against as charging. However, this always outputs false. I’ve tried "charging" too, but this also outputs false. Why can’t it do simple string equality checks?

In the side bar if you read the info for the node it states

State Typestring
Convert the state of the entity to the selected type. Boolean will be converted to true based on if the string is equal by default to (y|yes|true|on|home|open). Original value stored in msg.data.original_state

Meaning in order for it to make the conversion the string must be one of the following (y|yes|true|on|home|open), charging is not one of the string options available for auto-conversion.

Oh I see… so if I keep it as the String type, as long as I hook up an output only on the ‘true’ output node (the top one, presumably), it should do what I want?

You’d need to turn off the boolean as well. So true output to on, the other to off.

image

Ok… let’s say I have two poll state nodes. If they’re both true, I want to do a time check (is it 8pm yet). If that’s true, I want to send an event. How would I hook that up?

use single Events: State node to intercept changes of all needed entitities. Use its output to trigger Get Entities node working in Count mode. Configure this one to filter out the same entities but in active state. The output will be a number of active entities. If >0 than all are active

Why do you poll every minute?
That is not a good way to create an automation.

I’m sure there is a better way, post what you have and tell us what your end goal is and we can help you.

This:

I bet @Hellis81 is right

I want a notification sent to my phone if my phone’s battery is below 40% and not in charging state any time after 9pm.

share your flow

something like this will do it. Use the battery state as the trigger instead of polling.

Im not sure the orange node is installed by default in NR. If not, make soure you install node-red-contrib-time-range-switch. Top right menu (the three bars), manage palette, install: node-red-contrib-time-range-switch.

[{"id":"8fdce18f.e26f1","type":"trigger-state","z":"937ef78.ec6b408","name":"triggers","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.battery","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<=","comparatorValueDatatype":"num","comparatorValue":"40","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","comparatorType":">","comparatorValueDatatype":"num","comparatorValue":"40","propertyValue":"old_state.state"},{"targetType":"entity_id","targetValue":"sensor.charging_discharging","propertyType":"current_state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"charging","propertyValue":"new_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"num","enableInput":false,"x":230,"y":240,"wires":[["1abd5ad.69921a5"],[]]},{"id":"1abd5ad.69921a5","type":"time-range-switch","z":"937ef78.ec6b408","name":"","lat":"","lon":"","startTime":"21:00","endTime":"00:00","startOffset":0,"endOffset":0,"x":380,"y":240,"wires":[["c286749a.1f8888"],[]]},{"id":"c286749a.1f8888","type":"api-call-service","z":"937ef78.ec6b408","name":"Notify","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":540,"y":240,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I’m quite sure it is.
That is a much better and solution.

if you are really fussy

[{"id":"1202282f.6654c8","type":"trigger-state","z":"937ef78.ec6b408","name":"battery","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.battery","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<=","comparatorValueDatatype":"num","comparatorValue":"40","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","comparatorType":">","comparatorValueDatatype":"num","comparatorValue":"40","propertyValue":"old_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"num","enableInput":false,"x":190,"y":220,"wires":[["54f3e9be.77aad8"],[]]},{"id":"56e88892.636438","type":"api-call-service","z":"937ef78.ec6b408","name":"Notify","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":650,"y":180,"wires":[[]]},{"id":"63ead073.080de","type":"api-current-state","z":"937ef78.ec6b408","name":"battery <= 40?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"40","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.battery","state_type":"num","blockInputOverrides":true,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":320,"y":180,"wires":[["49265a98.b46b44"],[]]},{"id":"49265a98.b46b44","type":"api-current-state","z":"937ef78.ec6b408","name":"!= charging?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"charging","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"sensor.charging_discharging","state_type":"str","blockInputOverrides":true,"outputProperties":[],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":510,"y":180,"wires":[["56e88892.636438"],[]]},{"id":"54f3e9be.77aad8","type":"time-range-switch","z":"937ef78.ec6b408","name":"","lat":"","lon":"","startTime":"21:00","endTime":"00:00","startOffset":0,"endOffset":0,"x":330,"y":220,"wires":[["49265a98.b46b44"],[]]},{"id":"160b1998.87c046","type":"inject","z":"937ef78.ec6b408","name":"at 21:01","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"01 21 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":180,"wires":[["63ead073.080de"]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I’m not sure I understand this node; I’ve tried to make it work like this:

The two sensors I want to listen to are sensor.oneplus_a5000_battery_level and sensor.oneplus_a5000_battery_state. I didn’t know what to put in the ‘main’ entity box, but it required something, so I put sensor. I hook up the ‘allowed’ endpoint to a debug msg node, but no results, even though it technically should pass both conditions (my phone is under 40% now and not charging).

It didn’t do anything because that main box is supposed to include all the triggers. Since you set it to “exact” and you have no entity called exactly “sensor”, it will never trigger. You should set that first node like this:

[{"id":"b85d3092.ce641","type":"trigger-state","z":"937ef78.ec6b408","name":"triggers","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.oneplus_a5000_battery_level","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<=","comparatorValueDatatype":"num","comparatorValue":"40","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","comparatorType":">","comparatorValueDatatype":"num","comparatorValue":"40","propertyValue":"old_state.state"},{"targetType":"entity_id","targetValue":"sensor.oneplus_a5000_battery_state","propertyType":"current_state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"charging","propertyValue":"new_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"num","enableInput":false,"x":390,"y":560,"wires":[["e26ab3eb.3ac2"],[]]},{"id":"e26ab3eb.3ac2","type":"time-range-switch","z":"937ef78.ec6b408","name":"","lat":"","lon":"","startTime":"21:00","endTime":"00:00","startOffset":0,"endOffset":0,"x":540,"y":560,"wires":[["6d1f186e.e44d48"],[]]},{"id":"6d1f186e.e44d48","type":"api-call-service","z":"937ef78.ec6b408","name":"Notify","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":700,"y":560,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

You shouldn’t delete any of the two <= 40 or > 40 conditions. If you remove the previous entity condition, it’ll notify you every time battery drops below 40%, which is from 40 to 39, from 39 to 38, from 38 to 37 and so on… The second flow is a bit more accurate since it’ll notify at 21:01 if battery is below 40% and not charging. The first flow won’t notify you until your battery drops whithin that time range. I’d use the second one.