Error while executing automation automation.toggle_lights_on_and_off

Hey All,

I’ve configured a Xiaomi button to trigger different lights on click, and while the automation runs, I keep getting an error in my home assistant logs? Just wanted to see if anyone knows why I’m getting this error and if I need to worry about it. Like I said, my automation runs no problems.

This is the error:
Error while executing automation automation.toggle_lights_on_and_off. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

And this is my code.

- id: 'masterswitch'
  hide_entity: true
  alias: Toggle lights on and off
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/0x00158d00034039aa
  condition: []
  action:
    service: light.toggle
    data_template:
      entity_id: >
        {% if trigger.payload_json.click == "single" %}
          light.upstairs_lounge
        {% elif trigger.payload_json.click == "double" %}
          light.upstairs_bedroom
        {% elif trigger.payload_json.click == "triple" %}
          light.upstairs_lounge, light.upstairs_bedroom
        {% endif %}

Probably because your payload is not “single”, “double” or “triple”. You need an {% else %} for this case that provides another entity id.

i would also down load MQTT Explorer or mqttfx and check and what the

zigbee2mqtt/0x00158d00034039aa is sending

the way I would do it build a sensor

some like thig

  - platform: mqtt
    name: "this_click"
    state_topic: "zigbee2mqtt/0x00158d00034039aa"

then the

- id: 'masterswitch'
  hide_entity: true
  alias: Toggle lights on and off
  trigger:
  - entity_id: sensor.this_click
    platform: state
  condition: []
  action:
    service: light.toggle
    data_template:
      entity_id: >
        {% if trigger.to_state.state == "single" %}
          light.upstairs_lounge
        {% elif trigger.to_state.state == "double" %}
          light.upstairs_bedroom
        {% elif trigger.to_state.state == "triple" %}
          light.upstairs_lounge, light.upstairs_bedroom
        {% endif %}

this is off top of head not tested

1 Like

I’m not sure I understand what you mean? This is some examples from the zigbee2mqtt logs:

zigbee2mqtt:info 9/6/2019, 9:48:32 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":68,"battery":100,"voltage":3022,"click":"single"}'
zigbee2mqtt:info 9/6/2019, 9:49:06 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":63,"battery":100,"voltage":3022,"click":"double"}'
zigbee2mqtt:info 9/6/2019, 9:49:10 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":39,"battery":100,"voltage":3022,"click":"triple"}'

Sorry, I probably should have said that I’m using the zigbee2mqtt addon. So I can see what is being sent, and it all looks correct to me?

Been playing around a bit more and realised there’s already a sensor, I just didn’t realise. I’m still getting my head around entities.

So this is now my trigger code.


  trigger:
  - platform: state
    entity_id: sensor.0x00158d00034039aa_click

And this is my template:

{% if trigger.to_state.state == "single" %}

Thanks for your input. That really helped. :smile:

My issue however still stands. I’m still getting that error in my logs. My actions still execute, but home assistant just doesn’t like something and I’m not sure what.

with out see the logs it hard to fix

the error mite just be HA is seeing the Mqtt Stuff but there nothing to attach it to

looking at the data more

you could add these

  - platform: mqtt
    name: " switch battery"
    state_topic: "zigbee2mqtt/0x00158d00034039aa"
    value_template: '{{value_json.battery}}'
   

think the above should work thats off top of head

So my automation is running as it should. All I’m trying to work out is why I’m getting the error.

This is my automation now (I’ve changed it slightly to allow play/pause of my media player)

- id: 'masterswitch'
  hide_entity: true
  alias: Toggle lights on and off
  trigger:
  - platform: state
    entity_id: sensor.0x00158d00034039aa_click
  condition: []
  action:
    service_template: >
      {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
        media_player.media_play_pause
      {% else %}
        light.toggle
      {% endif %}
    data_template:
      entity_id: >
        {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
          media_player.the_ultra
        {% elif trigger.to_state.state == "double" %}
          light.upstairs_lounge
        {% elif trigger.to_state.state == "triple" %}
          light.upstairs_bedroom
        {% elif trigger.to_state.state == 'quadruple' %}
          light.upstairs_lounge, light.upstairs_bedroom
        {% endif %}

This is my zigbee2mqtt log when I click something:

  zigbee2mqtt:info 9/7/2019, 1:16:52 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":97,"battery":100,"voltage":3012,"click":"double"}'
  zigbee2mqtt:info 9/7/2019, 1:16:52 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":97,"battery":100,"voltage":3012,"click":""}'

This is my developer tools info log when I click something

Log Details (ERROR)
Sat Sep 07 2019 13:16:46 GMT+1200 (New Zealand Standard Time)
Error while executing automation automation.toggle_lights_on_and_off. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

And if I get it to show my full logs

2019-09-07 13:18:38 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.toggle_lights_on_and_off. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

So just to confirm, the automation does run, it does to what I want it to, but I’m getting the above error in my logs as well. Is there any other information I can provide that might help? Or is it just something I don’t need to worry about since my automation runs anyway?

mmmmm that beater

my uderstanding of it iis
then
HA see

 zigbee2mqtt:info 9/7/2019, 1:16:52 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":97,"battery":100,"voltage":3012,"click":""}'

a blank click the automation starts but

there is no

trigger.to_state.state == ""

so there is no entity_id so it errors

hope this is clear as mud

1 Like

Ah, that actually makes so much sense. Thank you. So my thinking is maybe if I add a condition that the automation doesn’t run if the state is empty. I’ve tried the following but it doesn’t seem to work. Any suggestions?

  condition:
    # Condition #1: Only run if click has a value
    condition: template
    value_template: "{{ not is_state('trigger', '') }}"

SNAP

but would it be sometrhing like

value_template: "{{ not is_state('trigger.to_state.state', '') }}"

cause we want to check the trigger.to_state.state bit

just wing it here off top of head

Hmm, no I just tried that with no luck.

I’ve also tried:

value_template: "{{ not is_state('sensor.0x00158d00034039aa_click', '') }}"

EDIT

I just got it working! Thanks!

- id: 'masterswitch'
  hide_entity: true
  alias: Toggle lights on and off
  trigger:
  - platform: state
    entity_id: sensor.0x00158d00034039aa_click
  condition:
    # Condition #1: Only run if click has a value
    condition: template
    value_template: "{{ not trigger.to_state.state == '' }}"
  action:
    # Action 1: Play/Pause media player if it is running
    # Action 2, 3, 4: Toggle the defined light
    service_template: >
      {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
        media_player.media_play_pause
      {% else %}
        light.toggle
      {% endif %}
    # Action 1: Play/Pause media player if it is running
    # Action 2: Toggle the lounge light
    # Action 3: Toggle the bedroom light
    # Action 4: Toggle the lounge AND bedroom light
    data_template:
      entity_id: >
        {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
          media_player.the_ultra
        {% elif trigger.to_state.state == "double" %}
          light.upstairs_lounge
        {% elif trigger.to_state.state == "triple" %}
          light.upstairs_bedroom
        {% elif trigger.to_state.state == 'quadruple' %}
          light.upstairs_lounge, light.upstairs_bedroom
        {% endif %}

Edit: Apparently you can’t put comments in templates. Fixed to move comments out of templates.

Here’s an alternative technique that doesn’t use a condition. Add a final {% else %} to the data_template as shown here:

    data_template:
      entity_id: >
        {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
          media_player.the_ultra
        {% elif trigger.to_state.state == "double" %}
          light.upstairs_lounge
        {% elif trigger.to_state.state == "triple" %}
          light.upstairs_bedroom
        {% elif trigger.to_state.state == 'quadruple' %}
          light.upstairs_lounge, light.upstairs_bedroom
        {% else %}
          light.none
        {% endif %}

If anything other than a single/double/triple/quadruple click is detected, the template will produce:

entity_id: light.none

This entity does not exist but Home Assistant won’t complain about running a service for a non-existent entity. However, it will complain if requested to run a service for a blank entity.

3 Likes

of course that makes cents

its the value not the state we are checking

good feeling when a plan comes together a

1 Like

Thanks! That also makes sense. I was wondering how to handle that when other states slip past like long_hold. Thank yo. :smile:

Instead of monitoring the zigbee2mqtt log, I recommend you use an MQTT client like MQTT Explorer to examine a topic’s payloads. I’m quite certain it would’ve revealed the fact that sometimes the sensor’s payload is blank. MQTT Explorer is an excellent tool for debugging MQTT problems.

1 Like

Ooh that looks super helpful. Thanks. Just having a play with that now. :smile:

EDIT
Do you know how I go about connecting to my system? I’ve installed the Windows application and I’m trying to connect using the settings setup I defined in my MQTT broker, but it keep saying read ECONNRESET.

UPDATE
I just had to disable TLS.

Let’s say you are using the version with a condition that rejects a blank to_state. What happens if to_state is a single-click but the media_player is currently off?

  • The service_template will be light.toggle.
  • The data_template will be … nothing. entity_id will be blank.

I’ve updated my data template to match the code you provided. I had thought it would just continue through the if statements until it hit the else and triggered light.none, but now I’m getting my error again which makes me think maybe not?

If you’re using the version I suggested (with the final ‘else’) then, yes, evaluation of the template will continue beyond the first test and reach the final ‘else’. It won’t produce a blank entity_id.

However, you claim this isn’t what’s happening and there’s an error message. Please post your complete automation and the error message.

Ok this is my updated code:

- id: 'masterswitch'
  hide_entity: true
  alias: Toggle lights on and off
  trigger:
  - platform: state
    entity_id: sensor.0x00158d00034039aa_click
  condition:
    # Condition #1: Only run if click has a value
    condition: template
    value_template: "{{ not trigger.to_state.state == '' }}"
  action:
    # Action 1: Play/Pause media player if it is running
    # Action 2, 3, 4: Toggle the defined light
    service_template: >
      {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
        media_player.media_play_pause
      {% else %}
        light.toggle
      {% endif %}
    # Action 1: Play/Pause media player if it is running
    # Action 2: Toggle the lounge light
    # Action 3: Toggle the bedroom light
    # Action 4: Toggle the lounge AND bedroom light
    data_template:
      entity_id: >
        {% if (trigger.to_state.state == "single") and (is_state('media_player.the_ultra', 'on')) %}
          media_player.the_ultra
        {% elif trigger.to_state.state == "double" %}
          light.upstairs_lounge
        {% elif trigger.to_state.state == "triple" %}
          light.upstairs_bedroom
        {% elif trigger.to_state.state == 'quadruple' %}
          light.upstairs_lounge, light.upstairs_bedroom
        {% else %}
          light.none
        {% endif %}

My zigbee2mqtt log

  zigbee2mqtt:info 9/7/2019, 2:31:00 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":52,"battery":100,"voltage":3012,"click":"single"}'
  zigbee2mqtt:info 9/7/2019, 2:31:00 PM MQTT publish: topic 'zigbee2mqtt/0x00158d00034039aa', payload '{"linkquality":52,"battery":100,"voltage":3012,"click":""}'

My Developer Tools Info Log:

Error while executing automation automation.toggle_lights_on_and_off. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

EDIT

Sorry! Rookie move! Forgot to reload my automation… My bad. Thanks. This works! :smile:

1 Like