Compare two values

Can someone please show me the right direction how to compare two values in NR?

If state_attr('sensor.nordpool_kwh_se4_sek_3_095_0', 'current_price') >=(state_attr('sensor.nordpool_kwh_se4_sek_3_095_0', 'today') | sort)[2] Then True Else False

I can not import the code you have pasted - explain what you want to achieve ?

If it is a siple compare then use a switch node - as that is exactly what it is an IF THEN ELSE flow control object

Craig

In the event state set the sensor as the entity and in if state use

Thanks a lot for the help! I’m struggling with the syntax for getting record #3 for the today attribute which was found in HA with (state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_0’, ‘today’) | sort)[2]. Can you please help me with the correct syntax for this?

Set the entity to the current state node and post the debug output.

[{"id":"a9b6e03edc087e0e","type":"inject","z":"f80b6c338afd5483","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":980,"wires":[["3bf5ee979e3b44e2"]]},{"id":"3bf5ee979e3b44e2","type":"api-current-state","z":"f80b6c338afd5483","name":"","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":380,"y":980,"wires":[["adfe42c54749351e"]]},{"id":"adfe42c54749351e","type":"debug","z":"f80b6c338afd5483","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":620,"y":980,"wires":[]}]

Here is the debug output:

Today I have this binary sensor which evaluates the current price against the third lowest price for the day. Now I want to get the third lowest price for the day in NR (state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_0’, ‘today’) | sort)[2] and save it to a helper called “input_number.car_daily_price_limit”

binary_sensor:

  • platform: template
    sensors:
    run_on_daily_low_car:
    friendly_name: “Run on daily low - car”
    value_template: >-
    {% if state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_0’, ‘current_price’) <= (state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_0’, ‘today’) | sort)[2] %}
    True
    {% else %}
    False
    {% endif %}
    attribute_templates:
    limit: >-
    {{ (state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_0’, ‘today’) | sort)[2] }}
    current: >-
    {{ state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_0’, ‘current_price’) }}

If you hover next to the number you want the will be a button copy path. That path should be entity().attributes.today[2]

In Node-RED I believe the full JSONata expression you require (for the HA web socket node) is

$entity().attributes.current_price >= $sort($entity().attributes.today)[2]

In JSONata:
$entity() is the special HA function that returns the entity currently referred to by the node
.attributes.today gets the ‘today’ attribute, which in your case is an array (that you want to sort)
$sort(array) sorts the array (in ascending order, there is an optional parameter to reverse the sort)
[2] gets the 3rd entry in the sorted array

As this is a predicate expression, the result will be Boolean, so no further work is required to turn this into True or False

In JSONata, the various functions can be used either as function calls, eg $sort(array) with optional parameters, or they can be used as filters, so

$entity().attributes.today ~> $sort()

will do much the same - in this situation the first parameter of the sort function (the array) is assumed to be the output of the previous operation. This returns a sorted array, so getting at the 3rd element requires [2], and I believe this would require

($entity().attributes.today ~> $sort())[2]

Where multiple operations are required I guess it is personal preference whether to use nested functions or a chain of filters.

I hope this helps

2 Likes

Thank you very much for a detailed explanation!