Automation help (run once?)

Probably because I didn’t spend time writing the re-enable automation. :slight_smile:

1 Like

Triggering on service calls was a hard concept for me to get. I mean, look at that template to get the entity_id lol! Figured it would be the hardest part of the whole thing.

1 Like

I was prepared to answer a follow-up, but you beat me to it.

testing a related templated automation myself as we write:

  - alias: 'Github persistent notification dismissal turns off boolean'
    trigger:
      platform: event
      event_type: call_service
      event_data:
        domain: persistent_notification
        service: dismiss
    condition:
      #check for 'github' so this only fires for selected notification dismissals
      condition: template
      value_template: >
        {{'github' in trigger.event.data.entity_id.split('.')[1]}}
    action:
      service: input_boolean.turn_off
      data_template:
        entity_id: >
          input_boolean.{{trigger.event.data.entity_id.split('.')[1]}}

wouldn’t that be an easier way for the condition?

{{'50758014840d8e918614' in trigger.event.data.entity_id.split('.')[1]}}

I ‘ask’ because I am not sure of the correct trigger fields…

That was fast from both of you.
I do understand the first part. The “call service” it’s difficult to grasp not to mention to actually implement.

Ignoring the syntax of ‘to_dict()’ vs string splitting, the difference is this.

Service Data:
https://www.home-assistant.io/docs/configuration/events/#event-call_service

Trigger Data:
https://www.home-assistant.io/docs/automation/templating/#event

The way I’ve done it, trigger.event.data will return the 1st linked url which only defines those things…service_data being one of them. data.entity_id ‘might’ be in that data, or it might not be. It will depend on the service and/or integration as to what data is put in trigger.event.data. In your case, you seem to be getting lucky that ‘entity_id’ is defined there. It might not always be true.

It’s similar to how you call a service. You can do the following 2 things, both valid.

- service: light.turn_on
  entity_id: light.my_light
- service: light.turn_on
  data:
    entity_id: light.my_light

Technically, we should all be doing the 2nd one according to the documentation. But for some reason, the first one works. Maybe home assistant is automatically copying the entity_id into the service data for us. My other guess is, if you were to look at this event data, trigger.event.data.entity_id would not exist for the second call, but will for the first.

Actually, I want to test this…brb.

Ok, here’s the difference. Here’s my test script.

light_service_test_1:
  sequence:
    - service: light.turn_on
      entity_id: light.office_dimmer_switch
    - delay: "00:00:01"
    - service: light.turn_on
      data:
        entity_id: light.office_dimmer_switch

Here are the capture service calls.

First event from script

{
    "event_type": "call_service",
    "data": {
        "domain": "light",
        "service": "turn_on",
        "service_data": {
            "entity_id": [
                "light.office_dimmer_switch"
            ]
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-04-14T15:25:03.530618+00:00",
    "context": {
        "id": "ecaf69738b454a6cb5edf46410144fc2",
        "parent_id": null,
        "user_id": "e39b6e3266eb4b3a81937449708a4f74"
    }
}

Second Event from script

{
    "event_type": "call_service",
    "data": {
        "domain": "light",
        "service": "turn_on",
        "service_data": {
            "entity_id": "light.office_dimmer_switch"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-04-14T15:21:14.262167+00:00",
    "context": {
        "id": "83b09dbae4474c858803377885214d63",
        "parent_id": null,
        "user_id": "e39b6e3266eb4b3a81937449708a4f74"
    }
}

So a few things.

  1. it looks like by not specifying the service data, it auto generated a service data LIST with the entity id in the list.

  2. in no cases would ‘trigger.event.data.entity_id’ exist. At least for this service. The persistent_notification domain might be different.

1 Like

no its no different, your findings are correct and appreciated!:

homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'dict object' has no attribute 'entity_id'
2020-04-14 17:29:19 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'entity_id'

struggling to find the correct template though, because I added service_data and it still gives me the identical error… I would have thought the service_data entity_id would be the persistent_notification.1234 ?

think I need {{trigger.event.data.service_data.notification_id}}

Yeah, it’s tough to know what to put.

Open developer tools. Go to ‘events’ tab. At the bottom, type “call_service” and click ‘start listening’.

All service events will pop up here. Use this to determine what values will exist in the data you need.

ah, that is magic, I am ashamed to admit to never have understood/used that…

this is what gives:


thanks! learned a lot today. magic

1 Like

@jocnnor
I thought I have done this correct but no.
I have a sensor for win speed from openweather.

  - platform: openweathermap
    api_key: 'myapikey'
    name: openweather
    
    monitored_conditions:
      - wind_speed
      - rain

It populate the result in m/s but I need it in km/h. so I made a new sensor

# Weather convert wind speed
  - platform: template
    sensors:
      wind_ms_to_kmh:
        friendly_name: "Openweather in kmh"
        value_template: "{{(states ('sensor.openweather_wind_speed')| float *3.6 ) | round(2) }}"
    scan_interval: 60
    

which gives me the result I need
Annotation 2020-04-14 185227

but I get the following in the logs

Template sensor 'wind_ms_to_kmh' has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

I need to check 3 sensors and get the max value of them, and I did the following

  - platform: min_max
    type: max
    name: Wind Speed Automation
    entity_ids:
      - sensor.marousi
      - sensor.dark_sky_wind_speed
      - sensor.wind_ms_to_kmh

But I am getting result unknown. If I remove the sensor.wind_ms_to_kmh it is working ok.
What is wrong with the sensor.wind_ms_to_kmh?

You just need to add an entity to track to your template sensor:

# Weather convert wind speed
  - platform: template
    sensors:
      wind_ms_to_kmh:
        friendly_name: "Openweather in kmh"
        entity_id: sensor.openweather_wind_speed
        value_template: "{{(states ('sensor.openweather_wind_speed')| float *3.6 ) | round(2) }}"

I’m not sure why home assistant could not find this automatically. Are you sure the entity id is correct?

the entity id it was missing. I think now it is ok. I will watch it. thanks!

Ah! I see the problem.

There should be no space between states and (

states ('senso...

after a restart it is working
s1
but in a minute or 2 it is not
s2

I got the following

Log Details (WARNING)
Logger: homeassistant.components.min_max.sensor
Source: components/min_max/sensor.py:145
Integration: min_max (documentation, issues)
First occurred: 9:45:46 PM (1 occurrences)
Last logged: 9:45:46 PM

Units of measurement do not match for entity sensor.wind_speed_automation

I have tried with or without unit of mesurment but the problem remains

# Weather convert wind speed
  - platform: template
    sensors:
      wind_ms_to_kmh:
        friendly_name: "Openweather in kmh"
        unit_of_measurement: 'km/h'
        entity_id: sensor.openweather_wind_speed
        value_template: "{{(states('sensor.openweather_wind_speed')| float *3.6 ) | round(2) }}"
    scan_interval: 60

That error is pointing to a completely different sensor, sensor.wind_speed_automation, you have something to fix there. It’s a max/min sensor.

I posted wrong configuration earlier. it seems right but it isn’t.

  - platform: min_max
    type: max
    name: Wind Speed Automation
    entity_ids:
      - sensor.marousi
      - sensor.dark_sky_wind_speed
      - sensor.wind_ms_to_kmh

s3

What are the units of these three sensors (check the developer tools states menu)?

      - sensor.marousi
      - sensor.dark_sky_wind_speed
      - sensor.wind_ms_to_kmh

That was the problem. One sensor was missing the unit measurement.
Today (I hope) I will test the windy automation.
Thanks

1 Like

@jocnnor , @tom_l

Today I was able to test the automation. It was windy so the tent close as it should be.
However later when I tried to open the tent (without wind) the automation didn’t reset (reopen)
I am not sure how exactly the automation works, my cover states are below, and I also tried the service cover.cover_open but it didn’t work either.

Usually we open the tent manually, or through HA (not automated)
Just because the tent don’t report the exact state,
when it is partial open (always) it’s state is always unknown.
Can we use of the unknown status in automation?

- alias: Enable auto cover close
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: cover
        service: cover.open_cover
  condition:
    # Only do something if it's for the cover I care about
    condition: template
    value_template: "{{ trigger.event.as_dict()['data']['service_data']['entity_id'] == 'cover.50758014840d8e918614' }}"
  action:
    - service: homeassistant.turn_on
      entity_id: automation.Tent_windy

cover

this is working, so all ok.

- alias: Enable auto cover close
  trigger:
    platform: state
    entity_id: cover.50758014840d8e918614
    to: "unknown"
    
  action:
    - service: homeassistant.turn_on
      entity_id: automation.Tent_windy