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}]