Vague Conditional/Templating Error Statement?

Tags: #<Tag:0x00007f73927d12e0> #<Tag:0x00007f73927d1128>

Hi all. I am having an odd problem with my automation which was working, but then I changed it to involve a conditional AND statement. I think I might be confused at this point after looking at it for so long, and trying so many things in dev tools. I even found one github issue that was related as it also had a super vague error. https://github.com/home-assistant/core/issues/31461#issue-559836620

To summarise the intention, I set an alarm on my android phone (or the alarm changes itself depending on weekend/weekdays, etc), tasker then sends this new alarm via POST, grabbed by a webhook and recorded as an input_datetime. This new alarm is offset by a variable (to switch on heating X minutes before alarm), and then checked against the current time. This was the original conditional statement and worked fine. I then added in the AND statement that checks the outdoors temperature against the variable thermostat temp, and only if both cases are true will the action run.

### Morning Heating Time Set ###
- alias: 'Turn on heating in morning wrt alarm'
  trigger:
    - platform: template
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: >
          {% set hours = state_attr('input_datetime.time_alarm','hour')|int %}
          {% set minutes = state_attr('input_datetime.time_alarm','minute')|int %}
          {% set heat_on=(60*(hours*60 + minutes - states('input_number.heat_pre_alarm')|int))|timestamp_custom("%H:%M", false) %}
          {% if  states('sensor.time') == heat_on %}true{% endif %}
      - condition: template
        value_template: >
          {{ states('input_number.heating_threshold') | float > state_attr('weather.XXX','temperature')|float }}
  action:
    - service: switch.turn_on
      entity_id: switch.shelly_XXX

The resulting error statement when the config is checked:

Invalid config for [automation]: required key not provided @ data['trigger'][0]['value_template']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 14).

This has been driving me crazy so any help would be very appreciated.
Thanks,
Callum.

Where’s the trigger template?

The error message asks exactly that.

required key not provided @ data['trigger'][0]['value_template']. Got None. 

Means there is no template for the first trigger (counting from zero).

Ah I see. That was a bit of a hold over from the old automation:

- alias: 'Turn on heating in morning wrt alarm' 
  trigger:
    platform: template
    value_template: >
      {% set hours = state_attr('input_datetime.time_alarm','hour')|int %}
      {% set minutes = state_attr('input_datetime.time_alarm','minute')|int %}
      {% set heat_on=(60*(hours*60 + minutes - states('input_number.heat_pre_alarm')|int))|timestamp_custom("%H:%M", false) %}      
      {% if  states('sensor.time') == heat_on %}true{% endif %}
  action:
    - service: switch.turn_on
      entity_id: switch.shelly_xxx

Should I put the conditions indented within the trigger then?
Cheers, Callum.

Hi Tom, your comment made me realise my mistake. After coming back to HA automations after a while I had forgotten quite how things worked. I had moved my trigger condition into the condition for the automation, leaving the trigger bare. I have now reverted that and included the new addition in the automation condition, which has now passed the check. Thanks for the help, Tom!


### Morning Heating Time Set ###
- alias: 'Turn on heating in morning wrt alarm' 
  trigger:
    - platform: template
      value_template: >
        {% set hours = state_attr('input_datetime.time_alarm','hour')|int %}
        {% set minutes = state_attr('input_datetime.time_alarm','minute')|int %}
        {% set heat_on=(60*(hours*60 + minutes - states('input_number.heat_pre_alarm')|int))|timestamp_custom("%H:%M", false) %}      
        {% if  states('sensor.time') == heat_on %}true{% endif %}
  condition:
    condition: template
    value_template: >
      {{ states('input_number.heating_threshold') | float > state_attr('weather.stoatybearhaus','temperature')|float }}
  action:
    - service: switch.turn_on
      entity_id: switch.shelly_xxx

There’s a much simpler way to do it:

value_template: "{{ states('sensor.time') == states('input_datetime.time_alarm')[0:5] }}"

I could simplify the last line a bit, but the reason for the convoluted method is to offset the alarm time before the condition is tried. Perhaps if I offset using another datetime value rather than converting manually because of a plain number value?

You could use the input_datetime’s timestamp attribute to streamline the template:

### Morning Heating Time Set ###
- alias: 'Turn on heating in morning wrt alarm' 
  trigger:
    - platform: template
      value_template: >
        {% set ts = state_attr('input_datetime.time_alarm','timestamp') - ((states('input_number.heat_pre_alarm')|int) * 60) %}
        {{ states('sensor.time') == ts | timestamp_custom("%H:%M", false) }}
  condition:
    condition: template
    value_template: >
      {{ states('input_number.heating_threshold') | float > state_attr('weather.stoatybearhaus','temperature')|float }}
  action:
    - service: switch.turn_on
      entity_id: switch.shelly_xxx