Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 108497

Conditional actions based on entity_id?

$
0
0

Hi all,
I seem to be struggling with writing an automation, and I’m not sure where I’m going wrong.

I’m trying to write automations that, based on the entity_id which triggered the automation, will execute a different action. I’m trying to get away from copying automations and having much duplicates of the same thing. I’m also trying to write this in a separate package for separation.

What I’m currently working with:

timer:
  ## A timer for each light group
  light_gameroom:
    duration: '00:03:00'
  light_bedroom:
    duration: '00:03:00'

automation:
  ## Test conditional - Clear & Start timer when movement detected
- alias: lights movement start timer light_groups
  id: lights_movement_start_timer_light_groups
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.ccamdevice_4_1_111 # bedroom
    - binary_sensor.ccamdevice_3_1_108 # gameroom
    to: "on"
  action:
  - service: timer.cancel
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'binary_sensor.ccamdevice_3_1_108' %}
            timer.light_gameroom
        {% elif trigger.entity_id == 'binary_sensor.ccamdevice_4_1_111' %}
            timer.light_bedroom
        {% endif %}
  - service: timer.start
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'binary_sensor.ccamdevice_3_1_108' %}
            timer.light_gameroom
        {% elif trigger.entity_id == 'binary_sensor.ccamdevice_4_1_111' %}
            timer.light_bedroom
        {% endif %}
      duration: 10

  ## When timer is started, turn on light
- alias: lights timer started light_groups
  id: 'lights_timer_started_light_groups'
  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id:
      - timer.light_gameroom
      - timer.light_bedroom
  action:
  - service: light.turn_on
    data:
      # gameroom
      # bedroom
      entity_id: >
        {% if trigger.entity_id == 'timer.light_gameroom' %}
            light.yeelight_4
        {% elif trigger.entity_id == 'timer.light_bedroom' %}
            light.yeelight_5
        {% endif %}
      transition: 2
      brightness: 255
      kelvin: 3000

The first automation seems to work, and I can fire the timers as expected.

The second automation does not seem to work for various reasons.

Firstly, the triggers don’t seem to want to accept combined entity_ids, so I saw that it works with:

  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.light_gameroom
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.light_bedroom

Secondly, the actions section doesn’t seem to work, which I think is related to the trigger.entity_id not resolving anything. From the docs that seems like its supposed to work.

Is there anyway to resolve/read the trigger payload of an automations trigger?
I tried using the developer tools entities page and this is what I get when I listen to timer.started:

Event 0 fired 0:19:
{
    "event_type": "timer.started",
    "data": {
        "entity_id": "timer.light_gameroom"
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-05T23:19:30.939864+00:00",
    "context": {
        "id": "f9f55fe55d0de01a0bd1104d92a684d5",
        "parent_id": null,
        "user_id": null
    }
}

I’m kinda stuck, not sure what to do.

Any advice?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 108497

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>