Using variables from former action in automations?

Hello,
I was not able to find info on this and it may not be possible but I’d like to do 2 things with the same idea.

  1. In automations triggered via mqtt, I’d like to use the payload as a variable, say I send ‘22’ via mqtt, I’d like to use it as a target temperature in a climate.set_temperature action

  2. Consider these actions:

  - data:
      entity_id: climate.buro
      hvac_mode: heat
      temperature: 25
    entity_id: climate.buro
    service: climate.set_temperature
  - delay: 00:45
  - data:
      entity_id: climate.buro
      hvac_mode: heat
      temperature: 23
    entity_id: climate.buro
    service: climate.set_temperature

Say I wouldn’t want to set it back to an absolute 23 but instead use the temperature it was before we set it to 25, I would like to store the before-temeprature in a variable and reset it to that value after the delay

Is any of this possible (yet)?
Thanks in advance and best regards
Halest

  1. You can use {{ trigger.payload }} to recall the value received as the mqtt trigger for an automation.

  2. This might be different though, if you are talking about a previously set value. Not a trigger. For this there are a number of options.
    a) store it in an input number to be recalled later
    b) publish it to an mqtt topic to be recalled later
    c) use this: https://github.com/rogro82/hass-variables

If you were talking about the value received from an mqtt trigger this is what you need:

  - data:
      entity_id: climate.buro
      hvac_mode: heat
      temperature: 25
    entity_id: climate.buro
    service: climate.set_temperature
  - delay: 00:45
  - data_template:
      entity_id: climate.buro
      hvac_mode: heat
      temperature: '{{ trigger.payload }}'
    entity_id: climate.buro
    service: climate.set_temperature

Hello again tom_I,
as always, thank you very much for the always helpful answers, I do appreciate it indeed!
Regarding 1a) {{ trigger.payload }} is amazing! I did not see that in the docs! That will make my life easier, thanks!

Regarding 2, I’d be mostly interested in 2a. Is there a doc-page you could link me where I can study this?

Thank you!
Halest

There are examples of both setting and using the value of an input number at the bottom of the input_number integration document:

1 Like

Thanks, I have bookmarked it and will study and try it.
Have a great day

Also just FYI one thing to be careful of when using long delays in automations is that if the automation is re-triggered while waiting in the delay the delay will be skipped and execution of the actions will continue from the point after the delay (effectively cancelling the delay).

The usual way to get around this is to use two automations.

Automation 1: do something
Automation 2: so something 45 minutes after Automation 1 triggered

The actual method of determining how it is 45 minutes after Automation 1 triggered can vary. You can trigger on an entity being in a set state for 45 minutes or use a timer.

1 Like

Hello tom_I,
I studied the input_number and tried to put it in my automation in combination with the trigger.payload but am getting an error. Am I missing something?
One example would be this course of action:

- service: input_number.set_value
    data_template:
      entity_id: input_number.TempTemp
      value: "{{ state_attr('climate.buro', 'temperature') | int }}"
.....
- data:
      entity_id: climate.buro
      hvac_mode: heat
      temperature: "{{ states('input_number.TempTemp') | int }}"
    entity_id: climate.buro
    service: climate.set_temperature

And the next example would be

 - data:
      entity_id: climate.buro
      hvac_mode: heat
      temperature:  "{{ trigger.payload | int }}"
    service: climate.set_temperature
......
- data:
      entity_id: climate.buro
      hvac_mode: heat
      temperature:  "{{ states('input_number.TempTemp') | int }}"

Both of these throw basically the same error:

Invalid data for call_service at pos 2: expected float for dictionary value @ data['temperature']

I have also tried float instead of int but am basically getting the same error.
Or more verbose:

ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 190, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 273, in _handle_action
    await self._actions[_determine_action(action)](action, variables, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 355, in _async_call_service
    context=context,
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 97, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context
  File "/usr/src/homeassistant/homeassistant/core.py", line 1213, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 208, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 287, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 283, in _exec
    v = func(v)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 205, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 287, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/validators.py", line 285, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected float for dictionary value @ data['temperature']

data_template: not data: when using templates for values.

1 Like

Aw, there, too! I see. Thanks man, you’re a livesaver.
Happy holidays to you!

1 Like