Modifying trigger in automation

Hi,

no matter how much I play around with yaml, I am not getting used to the jinja stuff. Maybe someone can help me on this, I just do not find the solution:

I try to build very general automations and did set up multiple booleans with the “same name” as an existing light in the following scheme:
light.sofa → input_boolean.light_sofa

Using a dict as reference table, I assign a physical switch, e.g. doubleswitch_1
If doubleswitch_1 is pressed, I want to set the boolean to on.

To make it as efficient as possible, I came up with the following idea:

  - service: input_boolean.turn_on
    data:
      entity_id: >
        {%- set reftable = ({"binary_sensor.motion_occupancy" : "light.vz_group", "sensor.doubleswitch1_action" : "light.sofa"}) %}
        {{ "input_boolean." + (reftable[trigger.entity_id] | replace(".","_")) }}

In Dev tools the result is as intended, “input_boolean.light_sofa”, but as automation it is just not working…
Error in log is:

Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

Any idea on this?

I would check that trigger.entity_id is what you think it is when the automation fires and that it’s present as a key in your table. It might help to share the whole automation. Last, maybe try playing with the whitespace options like this:

      entity_id: >-
        {%- set reftable = ({"binary_sensor.motion_occupancy" : "light.vz_group", "sensor.doubleswitch1_action" : "light.sofa"}) -%}
        {{ "input_boolean." + (reftable[trigger.entity_id] | replace(".","_")) }}
  - service: input_boolean.turn_on
    data:
      entity_id: >
        {% set r = {"motion_occupancy": "light_vz_group", "doubleswitch1_action": "light_sofa"} %}
        input_boolean.{{ r[trigger.to_state.object_id] if trigger.to_state.object_id in r.keys() else 'none' }}
1 Like

Rob, thank you for your comments. Yes, it is for sure in the table, because I just set it up with one device for now, for testing. The strange thing in Home assistant is, the entity_id itself is not included in the automation trigger, in case of the switch e.g. sensor.doubleswitch1_action:

- id: '1605904386227'
  alias: Test_Switch
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: 70f5d5f7ca7d15f2ee8977cdfd1964f2
    type: action
    subtype: single_right
    discovery_id: 0x00158d00028fd7b7 action_single_right
  condition: []
  action:
  - service: input_boolean.turn_on
    data:
      entity_id: >
        {%- set reftable = ({"binary_sensor.motion_occupancy" : "light.vz_group", "sensor.doubleswitch1_action" : "light.sofa"}) %}
        {{ "input_boolean." + (reftable[trigger.entity_id] | replace(".","_")) }}
  mode: single

Taras, thank you, but this is not working…
Error message:

Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

I assume because I would additionally need the “conversion” from light.sofa to light_sofa.
Unfortunately, I cannot create input_booleans with periods in it, like “input_boolean.light.sofa”.

I will try to add this conversion and try again

What kind of trigger are you using in your automation? If it employs a State Trigger then trigger.to_state.object_id exists.

Please post the entire automation so we can provide you with better assistance. The template you posted contains unnecessary steps and can be refined. However, seeing the entire automation ensures we can provide you with more accurate suggestions.

Hi Taras,

I posted the full automation already three posts above: Modifying trigger in automation

As trigger I use a physical Aqara / Xiaomi switch over mqtt.

As I said, for strange reasons, the entity name of the switch I use is switch.doubleswitch1_action, but in the automation I created with the UI, this entity name is not used…

Ouch! My mistake; sorry about that. I see now that it’s a Device Trigger and not a State Trigger like I had assumed.

1 Like

No problem!

Ok thank you for that information, now I already learned something new, I thought there are just triggers.

And how do I use them? I mean, how do I know what values they have, like e.g. trigger.entity_id?
I just read the home assistant pages for mqtt device triggers and for automation triggers, but it is not clear to me…

EDIT: I just found that on the page Automation Templating they explain what values the triggers have, don’t know why it is not on the page for Automation Triggers. What I am still missing there is device trackers, what values do they have? And interestingly my Xiaomi switch says for trigger.platform ‘mqtt’ and my Xiaomi motion sensor says ‘device’. But both use mqtt…

EDIT 2: Thank you, now with your help I use the correct trigger value, and now my original and your code seems to work! Thank you :slight_smile:

1 Like