Data Template Issue

Hi I’m trying to dive into template and if logic but I can’t seem to get this right. Any suggestions?

Thanks!

 action:
    entity_id: climate.n3rd_st
    service: climate.set_temperature
    data_template: >
      {% if is_state("sensor.nest_hvac_action", "cooling") %}
       "temperature": "74"
      {% elif is_state("sensor.nest_hvac_action", "heating") %}
        "temperature": "66"
      {% else %}
        "temperature": "66"
      {% endif %}
  1. template goes inside the field, you can’t have a field inside the template.
  2. You’re also missing endif
  3. entity_id goes inside data section when dealing with some services, so it’s best to always place it there.
    service: climate.set_temperature
    data_template:
      entity_id: climate.n3rd_st
      temperature: >
        {% if is_state("sensor.nest_hvac_action", "cooling") %}
          74
        {% elif is_state("sensor.nest_hvac_action", "heating") %}
          66
        {% else %}
          66
        {% endif %}

Also, you can get rid of the elif because you’re setting everything else equal to the elif.

1 Like

Omg! That works and makes soooooo much sense. Thank you, I understand tempaltes a little better the if logic results are putting that value into temperature. Thank you! I’ve been working on this for awhile with no headway

Think of hte template output going into a single field and you should be good. Then it’s just the if /then logic from that point on.

1 Like

Thank you again! I did have a follow up question. Based of your logic I was thinking this should work but I’m getting an error. Is there a different way to do this when there are multiple attributes to set?

 Invalid data for call_service at pos 1: expected float for dictionary value @ data['target_temp_high']
  action:
    service: climate.set_temperature
    data_template:
      entity_id: climate.n3rd_st
      temperature: >
        {% if is_state("sensor.nest_hvac_mode", "cool") %}
          70
        {% elif is_state("sensor.nest_hvac_mode", "heat") %}
          63
        {% endif %}
      target_temp_high: >
        {% if is_state("sensor.nest_hvac_mode", "auto") %}
          76
        {% endif %}
      target_temp_low: >
        {% if is_state("sensor.nest_hvac_mode", "auto") %}
          65
        {% endif %}

you need to provide a value for every outcome. If you call the service and nothing is sent, you will get that error. So that error will occur when heat is set because nothing is going to temp_high or temp_low

Ah ok! I understand what you are saying. Is there way to use the state of the attribute to basically set it to what it already is?

Error while executing automation automation.turn_ac_heat_on_weekend. Invalid data for call_service at pos 1: expected float for dictionary value @ data['target_temp_high']

Is this possible?

action:
    service: climate.set_temperature
    data_template:
      entity_id: climate.n3rd_st
      temperature: >
        {% if is_state("sensor.nest_hvac_mode", "cool") %}
          70
        {% elif is_state("sensor.nest_hvac_mode", "heat") %}
          63
        {% else %}
        states('sensor.nest_target_temp')
        {% endif %}
      target_temp_high: >
        {% if is_state("sensor.nest_hvac_mode", "auto") %}
          76
        {% else %}
        states('sensor.nest_target_temp_high')
        {% endif %}
      target_temp_low: >
        {% if is_state("sensor.nest_hvac_mode", "auto") %}
          65
        {% else %}
        states('sensor.sensor.nest_target_temp_low')
        {% endif %}

Yes but you need to use {{ }} for your states() functions.

Ah, I was super close! I also found adding | float got it working in addition to adding “]” Thank you! I got this working now with your help. Posting the solution down here as well.

This could be used for the nest thermostat

  action:
    service: climate.set_temperature
    data_template:
      entity_id: climate.n3rd_st
      temperature: >
        {% if is_state("sensor.nest_hvac_mode", "cool") %}
          70
        {% elif is_state("sensor.nest_hvac_mode", "heat") %}
          63
        {% else %}
        {{ states('sensor.nest_target_temp') | float  }}
        {% endif %}
      target_temp_high: >
        {% if is_state("sensor.nest_hvac_mode", "auto") %}
          76
        {% else %}
        {{ states('sensor.nest_target_temp_high') | float }}
        {% endif %}
      target_temp_low: >
        {% if is_state("sensor.nest_hvac_mode", "auto") %}
          65
        {% else %}
        {{ states('sensor.sensor.nest_target_temp_low') | float }}
        {% endif %}

Hi,

Have problem with this. Don’t know what is the problem. Please help.

service: climate.set_temperature
data_template:
  entity_id: climate.room_1
  temperature: >
    {% if states('sensor.0x00158d0002b57f51_temperature') | float < 19 %}
      22
    {% endif %}

an ‘if’ needs both an ‘else’ and an ‘endif’ (the elif’s are optional)
what happens when your temp = 19 or more ??
it will throw an error, not all errors are survivable

Ok so first I have this and not working too:

action:
  - service: climate.set_temperature
    data:
      temperature: >
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          19.5
        {% else %}
          22
        {% endif %}
      hvac_mode: >
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          auto
        {% else %}
          heat
        {% endif %}
    entity_id: climate.room_1

No, you had : -

So you have been telling porkies

Well, I don’t think action is ever ‘not indented’
so indent all the rest the same addition
And entity_id needs 2 more spaces before it

Of course I’ve not seen your whole automation so I’m just guessing

Can you enlighten me as to why people only show a snippet of the whole code that is giving them a problem ? (about 30% of the time the error lies elsewhere) I’m genuinely interested.

Here is the full code and get this error:
Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data[‘temperature’]
Thats why sent only that climate.set.

alias: Heating xiaomi trigger
description: ''
mode: single
trigger:
  - platform: state
    entity_id: sensor.0x00158d0002b57f51_temperature
  - platform: time
    at: '01:00:00'
condition:
  - condition: time
    after: '01:00:00'
    before: '05:30:00'
action:
  - service: climate.set_temperature
    data:
      temperature: >
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          19.5
        {% else %}
          22
        {% endif %}
      hvac_mode: >
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          auto
        {% else %}
          heat
        {% endif %}
    entity_id: climate.room_1
  - wait_template: "{{is_state('sensor.time', '05:31')}}"
  - service: climate.set_temperature
    data:
      temperature: 19.5
      hvac_mode: auto

And after save this automation go back and see this. Changed automatically. “>” to “|”

alias: Heating xiaomi trigger
description: ''
trigger:
  - platform: state
    entity_id: sensor.0x00158d0002b57f51_temperature
  - platform: time
    at: '01:00:00'
condition:
  - condition: time
    after: '01:00:00'
    before: '05:30:00'
action:
  - service: climate.set_temperature
    data:
      temperature: |
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          19.5
        {% else %}
          22
        {% endif %}
      hvac_mode: |
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          auto
        {% else %}
          heat
        {% endif %}
    entity_id: climate.room_1
  - wait_template: '{{is_state(''sensor.time'', ''05:31'')}}'
  - service: climate.set_temperature
    data:
      temperature: 19.5
      hvac_mode: auto
mode: single

{% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          19.5
        {% else %}
          22
        {% endif %}

If the first is true after do nothing. How to do that? Dont need to change to 19.5.
else 22 is ok.

I’m not at a workstation but although you have formatted the code correctly, the indentation looks very wrong. How was this written? (it does not look to be ui generated, so you must have copied this … ???)

What does your config check say.?

Where is this automation (it affects indentation) ?

If you do not want to change something after a state check then make the state check a condition and just bomb out (exit)

Ok so I remove the last 2 things wait template was the problem. Now it is working. Only need to edit that temperature template. What I need: “If temp sensor above 19 do nothing below set the temperature to 22.” How to do that? :slight_smile:

edit: not is it good there?

alias: Heating xiaomi trigger
description: ''
trigger:
  - platform: state
    entity_id: sensor.0x00158d0002b57f51_temperature
  - platform: time
    at: '23:00:00'
condition:
  - condition: time
    after: '19:00:00'
    before: '05:30:00'
action:
  - service: climate.set_temperature
    data:
      temperature: |
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          not
        {% else %}
          22
        {% endif %}
    entity_id: climate.room_1
  - service: climate.set_hvac_mode
    data:
      hvac_mode: |
        {% if states('sensor.0x00158d0002b57f51_temperature')|float > 19 %}
          auto
        {% else %}
          heat
        {% endif %}
    entity_id: climate.room_1
mode: single

Have a lot of error in the log.
Edited it and now have this. Working but don’t know why have a lot of error in the log:

alias: Heating xiaomi trigger
description: ''
trigger:
  - platform: state
    entity_id: sensor.0x00158d0002b57f51_temperature
  - platform: time
    at: '23:00:00'
condition:
  - condition: time
    after: '23:00:00'
    before: '05:30:00'
action:
  - service: climate.set_temperature
    data:
      temperature: |
        {% if states('sensor.0x00158d0002b57f51_temperature') | float < 19 %}
          22
        {% endif %}
    entity_id: climate.room_1
  - service: climate.set_hvac_mode
    data:
      hvac_mode: |
        {% if states('sensor.0x00158d0002b57f51_temperature') | float > 19 %}
          auto
        {% else %}
          heat
        {% endif %}
    entity_id: climate.room_1
mode: single

Now I have this. It is working but have a lot of error in log.

While executing automation automation.heating_xiaomi_trigger
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 404, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1026, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 242, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 250, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1399, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 218, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 341, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 337, in _exec
    v = func(v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 341, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 339, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/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']

and:

Already running
Heating xiaomi trigger: Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data['temperature']

These 3 lines are a problem for me (I don’t understand them) They should be indented differently.
Here is an example of one of my working automations : -

  #name: Light Kitchen Front Off
  - alias: au_light_kitchenf_off
    mode: single
    max_exceeded: silent
    trigger:
      - platform: state
        entity_id: light.fibdim2_kitf_level
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.sc_light_kitchenf_timer

yaml is hierarchical and the main thing is uniqueness so mine has - alias: au_light_kitchenf_off
the leading ‘-’ denotes that this is a list item with a ‘key’ (alias:) called au_light_kitchenf_off
Now ‘some’ peaple use the automation editor which creates a different header like : -

- id: 4782345
  alias: something used as a name

Here, the alias belongs to the id: as it is indented from it

Going back to mine again,
Each automation has to have : - a trigger and an action
Other things it can have are condition: and mode: (these are optional)
then trigger can have a list of trigger items (need to lead with a dash if there is more than 1)
ditto with conditions
under action: come services and ditto with them

Your automation breaks these rules and yet you say it works, I don’t know how
When I paste your automation into my config (VSC tries to correct the indenting but … )
The Config Check says this about your automation : -

Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/packages/lights/light_kitchen.yaml", line 67, column 3
expected <block end>, but found '-'
in "/config/packages/lights/light_kitchen.yaml", line 98, column 3

This should give you clues (in your instance) as to what is going wrong
Why will you not post your config check status ?

Edit: please note that the first line of my automation starts 2 spaces outdented (ie the ‘-’ counts)

Another way you could tackle this is to use choose : -


Or you could split the different actions into 2 scripts
eg
action:
  - service: >
      {% if states('sensor.0x00158d0002b57f51_temperature') | float < 19 %}
        script.script_if_less_than_19
      {% else %}
        script.script_if_not_less_than_19
      {% endif %}

Then write the two scripts accordingly

Thats why this yaml because I used the frontend yaml editor in automation.

And found the solution what I need another way. It is working good. Only one problem have in the log always get “already running” warning.:

alias: Heating xiaomi trigger
description: ''
trigger:
  - platform: state
    entity_id: sensor.0x00158d0002b57f51_temperature
condition:
  - condition: time
    after: '23:00:00'
    before: '16:00:00'
 action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.0x00158d0002b57f51_temperature
            below: '19'
        sequence:
          - service: climate.set_temperature
            data:
              temperature: 22
            entity_id: climate.room_1
      - conditions:
          - condition: numeric_state
            entity_id: sensor.0x00158d0002b57f51_temperature
            above: '19'
        sequence:
          - service: climate.set_hvac_mode
            data:
              hvac_mode: auto
            entity_id: climate.room_1
    default: []
mode: single