Help with automation template (run script if state)

I want to turn of the TV if it is responding to ping. For what I see this should be correct according to the documentation, but my config check say:
> Error loading /config/configuration.yaml: while scanning for the next token
> found character ‘%’ that cannot start any token
> in “/config/automations.yaml”, line 237, column 8

  - service: script.turn_on
    data_template:
      entity_id: >
      {% if is_state('binary_sensor.ping_finlux_tv_lan', 'on') %}
          script.harmonyhub_tv_powertoggle    
      {% endif %}

I am new to templating, so I might miss som basic stuff here…

you need to indent your template. It can’t be at the same tab position as the entity_id field. Also that template will error out when you execute it. It would be better to use a contiional statement before that action. Or make sure to provide the if statement with an else.

Services cannot be executed without attributes, they will produce errors in your logs.

Thanks, I got it working this way:

  - service: script.turn_on
    data_template:
      entity_id: >
        {% if is_state('binary_sensor.ping_finlux_tv_lan', 'on') %}
          script.harmonyhub_tv_powertoggle    
        {% endif %}

Can you maybe type out an example of how you think is the beast way to make this script?

1 Like
- condition: state
  entity_id: binary_sensor.ping_finlux_tv_lan
  state: 'on'
- service: script.harmonyhub_tv_powertoggle
1 Like

Well, this is a part of an automation. Then I need two automations

- id: '1571082133606'
  alias: Night routine
  description: Turn of TV and lights
  trigger: []
  condition: []
  action:
  - device_id: 726a476b141549d296027043a024bf63
    domain: light
    entity_id: light.zigbee_vindfang
    type: turn_off
  - device_id: 89641ece2d4b4590a9007f37e5a8ae28
    domain: light
    entity_id: light.zigbee_stue
    type: turn_off
  - device_id: df6f9eb2efdb405ea2fa0a97f95f592c
    domain: light
    entity_id: light.zigbee_soverom
    type: turn_off
  - device_id: 5a3aa8e6c4144d89b888ad6083b2a0fa
    domain: switch
    entity_id: switch.zwave_11
    type: turn_off
  - device_id: 8b5312929a804ae7bc4c9e431d566fa3
    domain: light
    entity_id: light.zwave_25
    type: turn_off
  - service: script.turn_on
    data_template:
      entity_id: >
        {% if is_state('binary_sensor.ping_finlux_tv_lan', 'on') %}
          script.harmonyhub_tv_powertoggle    
        {% endif %}

No, just do this

- id: '1571082133606'
  alias: Night routine
  description: Turn of TV and lights
  trigger: []
  condition: []
  action:
  - device_id: 726a476b141549d296027043a024bf63
    domain: light
    entity_id: light.zigbee_vindfang
    type: turn_off
  - device_id: 89641ece2d4b4590a9007f37e5a8ae28
    domain: light
    entity_id: light.zigbee_stue
    type: turn_off
  - device_id: df6f9eb2efdb405ea2fa0a97f95f592c
    domain: light
    entity_id: light.zigbee_soverom
    type: turn_off
  - device_id: 5a3aa8e6c4144d89b888ad6083b2a0fa
    domain: switch
    entity_id: switch.zwave_11
    type: turn_off
  - device_id: 8b5312929a804ae7bc4c9e431d566fa3
    domain: light
    entity_id: light.zwave_25
    type: turn_off
  - condition: state
    entity_id: binary_sensor.ping_finlux_tv_lan
    state: 'on'
  - service: script.harmonyhub_tv_powertoggle

The condition will stop the actions if it’s not met. Everything before will run fine.

I see, did not know I could use condition inside action