Thought this would be simple…trying to set away_temp of a climate entity. I can set the value in the config of the climate entity but after that I do not see a way to change it. No climate service call sets away_temp. Looked into using input_number.set_value but then this requires an entity_id of the entity so no way of writing to the attribute away_temp
Reason for this is that I use the away mode which then has automation triggered on entring away mode and then depending on other automations, sets the away_temp - or rather that’s what I am after.
So has anyone managed to set the away_temp at runtime using a service or some other mechanism?
Community member rodpayne created a python_script that can be used to set an entity’s state or attribute. It was originally created to set the state of a ‘read-only’ entity like a sensor but he enhanced it to allow setting an attribute.
The thing to keep in mind that is that when this python_script sets an entity’s state or attribute, that ‘forced’ value doesn’t survive a restart. For example, if you use it to set away_temp, on startup its value will revert to whatever you specified in its configuration.
On the topic of state machine, I have noticed that generic_thermostat component does not write its state and recover it on startup. Or at least that is what happens on my system. I can restart the core and all the temperature settings (i.e. setpoint) are lost. I get “unknown” as a value.
For those arriving here via search, this was the configuration.yaml to set the away_temp in my case as an automation but you get the idea and can adjust for your setup:
..
..
# Include python_script integration.
python_script:
..
..
automation:
# Copy current thermostat setpoint to 'away_temp' attribute
# to prevent changes when moving between 'away' and 'none' modes.
- alias: Thermostat setpoint to Away_Temp
initial_state: true
trigger:
platform: state
entity_id: climate.lounge
attribute: temperature
action:
service: python_script.set_state_or_attr
data_template:
entity_id: climate.lounge
away_temp: "{{ trigger.to_state.attributes.temperature }}"
..
..
Does the generic thermostat’ configuration contain the target_temp option? On startup it should (at the very least) set it to that value. If that option isn’t present then I imagine it will default to unknown until you set it to something (via the UI or a service call).
FWIW, Home Assistant periodically saves the state of a select few domains (and just prior to restart/shutdown) but definitely not all domains. This is from from an exhaustive list but just what comes to mind: input_boolean, automation, input_text, counter, etc. I don’t believe “climate” is one of them.
Indeed, target_temp is an attribute and this gets set to 18.0 in the yaml config. I’ve since started working on a python script (I ‘can do python’ for my sins) that reads the hass object and all state objects with attributes into a JSON and write this to a file which can be used on startup to (re)initialise my setup. There may well be native functionality in HA but I just haven’t found it. But as you say, there does seem to be some domains that are intrinsic to the state-machine but this is - again as you say - far from complete. I take it you meant “…far from an exhaustive list…”.
Hi @ninjaef, are you using a different python script that the one created by rodpayne ?
It seems that you are calling
service: python_script.set_state_or_attr
and rodpayne script is:
service: python_script.set_state
I’m trying to set away_temp using an input_number that is on my dashboard. So I can change the away temp.
Since my climate control system is both heater and cooler, during winter I do not want the same away temp than in summer !
So in order to avoid configuration change and server reboot every year, I would like to set that from an input number…
##### Automations for handling thermostat modes and parameters
##### When input_select.thermostat_mode is changed, then thermostat hvac mode is changed as well
- id: updating_thermostat_when_thermostat_mode_changed
alias: Updating Thermostat when input "Thermostat Mode" is changed
trigger:
- platform: state
entity_id: input_select.thermostat_mode
condition:
- condition: state
entity_id: 'input_boolean.heating_override_switch'
state: 'off'
action:
- service: climate.set_hvac_mode
data:
entity_id: climate.thermostat_salon,climate.thermostat_chambres
hvac_mode: "{{ states('input_select.thermostat_mode') }}"
##### When changing the value of input_number.away_temp, then thermostat away_temp is changed as well
- id: updating_thermostat_when_away_temp_changed
alias: Updating Thermostat away_temp value when input "Temp Absent" is changed
trigger:
- platform: state
entity_id: input_number.away_temp
action:
- service: python_script.set_state
data_template:
entity_id: climate.thermostat_salon,climate.thermostat_chambres
away_temp: "{{ states('input_number.away_temp') }}"
“updating_thermostat_when_thermostat_mode_changed” is working just well,
but “updating_thermostat_when_away_temp_changed” using the python script is doing nothing.
It seems that it’s just working on a state, but trying to change “away_temp” here.
If you need anything that could help me, it would be cool
edit: If you have anything that could help me, it would be cool
Sorry @ninjaef I meant “If you HAVE anything that could help me…”
What is the python script that you are using ?
I don’t see the function “set_state_or_attr” in rodpayne script.
Instead, there is the function “set_state” only. Maybe this is the reason why I cant’ update away_temp. Have you adapted rodpayne script in order to add “set attribute” capability to it?
I’m in the same situation than you here, I’m just trying to update away_temp dynamically and just can’t find a way to do it. Since you did it, i’m trying to understand how!
inputEntity = data.get('entity_id')
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
elif hass.states.get(inputEntity) is None:
logger.warning("===== unknown entity_id: %s", inputEntity)
else:
inputStateObject = hass.states.get(inputEntity)
logger.debug("inputStateObject = {0}".format(inputStateObject))
inputState = inputStateObject.state
logger.debug("inputState = {0}".format(inputState))
inputAttributesObject = inputStateObject.attributes.copy()
logger.debug("inputAttributesObject = {0}".format(inputAttributesObject))
# Loop all items from the 'data_template:' in config
for item in data:
logger.debug("LOOP: item = {0}".format(item))
# Get value
newAttribute = data.get(item)
logger.debug("LOOP: item = {0}; value = {1}".format(item,newAttribute))
if item == 'entity_id':
# Skip if config item is the entity name - we already have that
continue # already handled
elif item == 'state':
# If config item is 'state' then use the value
inputState = newAttribute
logger.debug("LOOP: inputState = {0}".format(inputState))
else:
# set the attribute to value
inputAttributesObject[item] = newAttribute
logger.debug("LOOP: inputAttributesObject[item] = {0}".format(newAttribute))
hass.states.set(inputEntity, inputState, inputAttributesObject)
copy that into a a file called set_state_or_attr.py in your CONFIG:python_scripts
the call you need to make is python_script.set_state_or_attr - why are you doing something else? No wonder it is not working. To be clear again, you must have the file set_state_or_attr.py in the in the subdirectory python_scripts within the homeassistant configuration directory (referenced as CONFIG:) above. If you do this it will work provided you make the correct call. Anything else and it will not work. You can test the service call in the Services tab from the Developer tools option on the front end.
Yes, this is what I do!
The call is working either with your script " set_state_or_attr" or with rodpayne’s “set_state”.
I have both files in my python_scripts folder.
Thank for the tips about Developer tools, I can see the service python_script.set_state_or_attr is here.
It does nothing when I call it…
I was trying to call set_state_test, something that does not exist, just to confirm that I had an error showing in my logs, which I did have, so that meant the call was working and the script running when correctly called…!
The only thing I don’t know is why it’s not working ! There is no reason