Store an attribute in a variable and use it later

Hi, I’m new to node red and I created some automations with the help of videos or guides I found on the internet. But there is currently one thing I cannot do. Here is what I want to do:
If I open a window, my sensor switches the value to “on”. When this trigger starts the temperature of my thermostat should be stored in a variable and then set to 10. After I close the window the stored value should be set back in the thermostat.

The yaml code is this (and it is working).

#Fenster offen
- alias: Fenster im Arbeitszimmer offen
  trigger:
    platform: state
    entity_id: binary_sensor.oeq2242812_state
    to: 'on'
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.current_temp_az
        value: '{{ states.climate.az_heizung.attributes.temperature | float }}'
    - service: climate.set_temperature
      data:
        entity_id: climate.az_heizung
        temperature: '10.0'
    - service: notify.notify
      data:
        message: Fenster wurde geöffnet

- alias: Fenster im Arbeitszimmer geschlossen
  trigger:
    platform: state
    entity_id: binary_sensor.oeq2242812_state
    to: 'off'
  action:
  - service: climate.set_temperature
    data_template:
      entity_id: climate.az_heizung
      temperature: '{{states.input_number.current_temp_az}}'

But how can I use this in node red. I’m very bad in programming so I want to use node red instead of programming in yaml. Can someone help me out please?

Thank you
Rick

Hi, I dont know if this is the best approach but could help. Using the function node you can set a global variables

Set global variable with the payload received, function node

global.set('test', msg.payload);
return msg;

Call the variable, function node

var x = global.get('test');
msg={payload:x};
return msg;

Screenshot-from-2020-06-13-16-19-54

Here is the code so you can test it. You can add the topic if you want on the msg=

[{"id":"b26c492e.22eba8","type":"function","z":"a25d62a6.cf3fe8","name":"variable set","func":"global.set('test', msg.payload);\nreturn msg;\n","outputs":1,"noerr":0,"x":410,"y":380,"wires":[["b1e9a837.d3d508"]]},{"id":"b1e9a837.d3d508","type":"debug","z":"a25d62a6.cf3fe8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":380,"wires":[]},{"id":"b53eb019.4cc1e8","type":"inject","z":"a25d62a6.cf3fe8","name":"","topic":"","payload":"12","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":380,"wires":[["b26c492e.22eba8"]]},{"id":"8ad1f355.5f59e","type":"function","z":"a25d62a6.cf3fe8","name":"variable call","func":"var x = global.get('test');\nmsg={payload:x};\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":460,"wires":[["e44a7208.2751d8"]]},{"id":"fb49c581.0aa4d","type":"inject","z":"a25d62a6.cf3fe8","name":"","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":460,"wires":[["8ad1f355.5f59e"]]},{"id":"e44a7208.2751d8","type":"debug","z":"a25d62a6.cf3fe8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":460,"wires":[]}]

Hope this can help you!

There are multiple ways to do this.

Here’s a simple node based approach that doesn’t require any javascript knowledge.

I used a desk lamp as a trigger, and a battery sensor in place of the thermostat setting.

The logic goes like this:

  • If the state of the lamp changes and the lamp state is now ON, [next node]
  • get the current value of the thermostat [next node]
  • then store that value using a change node where the output is set to either global, flow, or node depending upon your context (I used global). You’ll SET the value to global.variablename or to create an array of possibilities, global.variablename.value1, global.variablename.value2. The something and value in this is whatever you choose. You can see these values stored when you click the context data icon in the right hand pane (it’s usually hidden because the pane is too narrow.) [next node]
  • Call service to set thermostat to 10.
  • If, all the way back at the beginning, the sensor turns OFF [next node]
  • Change node to set msg.payload to the global variable you created in the first part, and now that your payload is stored in msg.payload, you can reference that [next node]
  • In another call service node in the data field.

The only piece of information you might be missing is the initial value of the variable. If your window happens to be open before the state is detected and updated in HA, then no variable will be set, so when it changes to off, the change node won’t know what to re-set the thermostat to. So, you could add some logic in there to switch based on the global variable value or lack thereof, and then set a default value to the thermostat. Or, you could do what I’ve done, which is to create a persistent array of states and services that are always stored, and are updated every minute.

[{"id":"a26461dd.9044","type":"change","z":"dc677967.105608","name":"Set Global Variable","rules":[{"t":"set","p":"tempValues.hall_thermostat","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":80,"wires":[["e311d175.78cc9"]]},{"id":"d1d1efda.e2fab","type":"server-state-changed","z":"dc677967.105608","name":"Desk Lamp On","server":"c7cbc76b.fd06a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.sonoff_desk_lamp_1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":120,"y":100,"wires":[["1a029808.2e4378"],["48b6b01.9e7425"]]},{"id":"1a029808.2e4378","type":"api-current-state","z":"dc677967.105608","name":"Get Thermostat","server":"c7cbc76b.fd06a8","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.battery_level","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":340,"y":80,"wires":[["a26461dd.9044"]]},{"id":"e311d175.78cc9","type":"api-call-service","z":"dc677967.105608","name":"Set Thermostat","server":"c7cbc76b.fd06a8","version":1,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"sensor.battery_level","data":"{\"value\":\"10\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":760,"y":80,"wires":[[]]},{"id":"d2456d14.9b223","type":"api-call-service","z":"dc677967.105608","name":"Set Thermostat","server":"c7cbc76b.fd06a8","version":1,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"sensor.battery_level","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":760,"y":160,"wires":[[]]},{"id":"48b6b01.9e7425","type":"change","z":"dc677967.105608","name":"Change Payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"tempValues.hall_thermostat","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":160,"wires":[["d2456d14.9b223"]]},{"id":"c7cbc76b.fd06a8","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Hi to all and sorry for my late reply. I did not get any notification that there are new answers. Thank you for your help, it’s working as intended!
The tip from Nicolas was the simplest but an easy way to use the function node for my request. Thanks Jon for your detailed explanation this was very helpful as well.
Thank you all!!

Cheers
Rick