Syntax with python variable

I am trying to create one automation to handle two different events. Reason is that I want to put one single switch on the GUI to turn it off and on for the end-user. The automation should only be running when the user selects it. The hard thing is that I want two different states to change the service usesd in the action.

- id: vedlikeholdslading
  alias: Vedlikeholdslading
  trigger:
    platform: template
    value_template: >-
      {% if states("sensor.ladeniva") | int < 75: action="on" %}
      {% elif states("sensor.ladeniva") | int > 80: action="off" %}
      {% endif %}
  action:
    service: >-
      {% if action == "on" %}
        switch.turn_on
      {% elif action == "off" %}
        switch.turn_off
      {% endif %}
    entity_id: switch.osram_plug_01_000d1a23_3

This does not work but I guess you get the idea. If sensor.ladenivå is below 75 then action service.turn_on and if it is above 80 then action service.turn_off. Is this even possible? Seems the declaration of the variabel “action” does not fly well. Could I save the state in a template perhaps? Any ideas?

Here is the error from config validation:

Invalid config for [automation]: Service {% if action == “on” %} switch.turn_on {% elif action == “off” %} switch.turn_off {% endif %} does not match format . for dictionary value @ data[‘action’][0][‘service’]. Got None invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘action’) for dictionary value @ data[‘trigger’][0][‘value_template’]. Got None

Thank you!

I don’t think you can define local variables that way in the value_template; that whole if statement looks like invalid python to me.
Why not use two state triggers - one that fires when the sensor value is below 75 and the other above 80, so something like:

  trigger:
    - platform: numeric_state
      entity_id: sensor.ladeniva
      below: 75
    - platform: numeric_state
      entity_id: sensor.ladeniva
      above: 80

And then you can interrogate the sensor value in the action again.

  action:
    service_template: >
      {% if states("sensor.ladeniva") | int < 75 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    data:
      entity_id: switch.osram_plug_01_000d1a23_3

Yes, thanks for this. Might be a good idea but I think I will need to duplicate the states(“sensor.ladeniva”) | int since the state is not integer. … let me test!

If anyone has more ideas please share but I will test this as soon as I get off work.

Thanks mate, this worked! :slight_smile:

- id: vedlikeholdslading
  alias: Vedlikeholdslading
  trigger:
    - platform: template
      value_template: '{{ states("sensor.ladeniva") | int < 75 }}'
    - platform: template
      value_template: '{{ states("sensor.ladeniva") | int > 80 }}'
  action:
    service_template: >-
      {% if states("sensor.ladeniva") | int < 75 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.osram_plug_01_000d1a23_3

But one question I have is. Why do I need the - in front of the platform. This one did NOT work:

- id: vedlikeholdslading
  alias: Vedlikeholdslading
  trigger:
    platform: template
    value_template: '{{ states("sensor.ladeniva") | int < 75 }}'
    platform: template
    value_template: '{{ states("sensor.ladeniva") | int > 80 }}'
  action:
    service_template: >-
      {% if states("sensor.ladeniva") | int < 75 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.osram_plug_01_000d1a23_3

In this last example only the second trigger worked. I cannot understand why…

Thanks in any case!!

With that syntax you basically define a list of independent triggers. When any of the triggers is activated, the automation is run.