Script sequence problem

Hi All,

I have follow script. But it seems this is not working because of the if statement maybe:

normal_normal_turn_on:
  alias: Normal Turn On
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.livingroom_all
        kelvin: 2700
        brightness_pct: 100

    - service: light.turn_on
      data:
        entity_id: light.balcony
        brightness_pct: 100
        effect: 0

    - service_template: >
        {% if states.binary_sensor.node_10.state == 'on' %} light.turn_off
        {% else %} light.turn_on
        {% endif %}
      data:
        entity_id: light.dressoir_all
        kelvin: 2700
        brightness_pct: 100

this script is called by follow automation. I have a if statement at end of this automation.
It check if there is a holiday. If so then it will call a script action.

Example see above. but it runs the first service call but then it stop and give a error:

Error executing script script.normal_normal_turn_on. Invalid data for call_service at pos 2: extra keys not allowed @ data[‘kelvin’]

Error while executing automation automation.scene_evening. Invalid data for call_service at pos 5: extra keys not allowed @ data[‘kelvin’]

- alias: Scene - Evening
  trigger:

  - platform: time_pattern
    minutes: /1

  - entity_id: device_tracker.geofency_peter
    platform: state
    to: home

  - entity_id: device_tracker.geofency_kay
    platform: state
    to: home

  - entity_id: sensor.harmony
    platform: state
    to: 'Off'

  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'

    - condition: state
      entity_id: input_boolean.scene_evening
      state: 'off'

    - condition: state
      entity_id: input_boolean.scene_goodnight
      state: 'off'

    - condition: state
      entity_id: sensor.harmony
      state: 'Off'

    - condition: or
      conditions:
        - condition: state
          entity_id: device_tracker.geofency_peter
          state: home
        - condition: state
          entity_id: device_tracker.geofency_kay
          state: home

  action:
  - service: notify.pushover
    data:
      message: Scene evening is activated

  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.scene_evening

  - service: input_boolean.turn_off
    entity_id:
      - input_boolean.scene_kodi
      - input_boolean.scene_tv
      - input_boolean.scene_iptv
      - input_boolean.scene_daylight
      - input_boolean.scene_appletv
      - input_boolean.scene_sexy

# NANOLEAF LIGHT
  - service: light.turn_on
    entity_id: light.nanoleaf
    data_template:
      brightness_pct: 50
      effect: >
        {% if states.input_select.select_holiday.state == "Christmas" %} Christmas
        {% elif states.input_select.select_holiday.state == "Easter" %} Easter
        {% elif states.input_select.select_holiday.state == "Halloween" %} Halloween
        {% elif states.input_select.select_holiday.state == "Kingsday" %} Kingsday
        {% elif states.input_select.select_holiday.state == "Bday" %} Fireplace
        {% else %} NorthernLight
        {% endif %}

# LIGHT
  - service_template: >
      {% if states.input_select.select_holiday.state == "Christmas" %} script.christmas_normal_turn_on
      {% elif states.input_select.select_holiday.state == "Halloween" %} script.halloween_normal_turn_on
      {% elif states.input_select.select_holiday.state == "Kingsday" %} script.kingsday_normal_turn_on
      {% elif states.input_select.select_holiday.state == "Bday" %} script.birthday_normal_turn_on
      {% elif states.input_select.select_holiday.state == "Nothing" %} script.normal_normal_turn_on
      {% endif %}

Can someone see the issue here?

Common mistake. The light.turn_off service does not accept kelvin or brightness_pct. There are different ways to correct this, but the easiest is to always call light.turn_on, but set brightness_pct to zero if you want to turn it off.

Strange, because it tested it first with a “test button”

this is working without any problems…

- alias: TEST BUTTON ON
  trigger:
    - entity_id: light.newkaku_014e5906_a
      platform: state
      to: 'on'

  action:
    - service_template: >
        {% if states.binary_sensor.node_10.state == 'on' %} light.turn_off
        {% else %} light.turn_on
        {% endif %}
      data:
        entity_id: light.dressoir_all
        kelvin: 2700
        brightness_pct: 50


- alias: TEST BUTTON ON
  trigger:
    - entity_id: light.newkaku_014e5906_a
      platform: state
      to: 'off'

  action:
    - service: light.turn_off
      entity_id: light.dressoir_all

Sorry, not following you. First, the second automation doesn’t seem to have anything to do with the issue. Next, the first automation will work fine when you manually trigger it if binary_sensor.node_10 is on. Did you manually trigger it when binary_sensor.node_10 was off?

1 Like

The second post was a test script. The if statement is same. The light.turn_on and light.turn_off part are the same. So why does the test script working and not within a script

I think maybe you didn’t appreciate the purpose of my last question. The first automation in what you just posted will not work if binary_sensor.node_10 is off, just like it won’t work (under the same condition) in the code in your original post.

So I ask again…

Sorry I didn’t answer your question. I didn’t test it when the sensor was off. But I did test it with sensor off with the test script.

That’s why I post the question why it is not working.:slight_smile:

The bottom line is, you can’t call light.turn_off and include kelvin in the service data.

As I said above, there are different ways to solve this. This is probably the easiest, but it may or may not work for you. Give it a shot…

    - service: light.turn_on
      data_template:
        entity_id: light.dressoir_all
        kelvin: 2700
        brightness_pct: "{{ 0 if is_state('binary_sensor.node_10', 'on') else 100 }}"

I’ll give it a try. Try tomorrow.
If the light turn on and then goes out it is a option. Not the nice one.

I came from domoticz. There you can put the whole action inside a if statement.
So the service, data, brightness.

For me seems a better way, because you include everything in a if statement to fire it or not. If 100 then turn on with brightness else if 0 then turn off

I’ll let you know how your solution will be…

this was the solution. Can you also tell me how I do multiple is_state?
Tried this but didn’t work:

"{{ 'orange' if is_state('input_select.select_holiday', 'Halloween' elif 'red' is_state('input_select.select_holiday', 'Christmas') else 'green' }}" 
"{{ 'orange' if is_state('input_select.select_holiday', 'Halloween') else 'red' if is_state('input_select.select_holiday', 'Christmas') else 'green' }}"
1 Like

Thank you!!

1 Like