Automation help (run once?)

Hi
I have the following automation which close the tent when is windy.
Since it is cover but it doesn’t have a state (on-off) I am wondering if there is a way to run only once (if the wind speed is more that 40 (km/h). I would like the automation to run again after someone opens the tent 5-6 hours or a day later.
Can this be done somehow?

  • I haven’t use any input boolean logic before so it feels right now difficult to implement.
#Tent Tent_windy        
- id: Tent_windyy
  alias: Tent windy
  trigger:
    platform: numeric_state
    entity_id: sensor.wind_speed_automation
    above: 40
  action:
    - service: cover.close_cover
      entity_id: cover.50758014840d8e918614
- id: Tent_windyy
  alias: Tent windy
  trigger:
    platform: numeric_state
    entity_id: sensor.wind_speed_automation
    above: 40
  action:
    - service: cover.close_cover
      entity_id: cover.50758014840d8e918614
    - service: homeassistant.turn_off
      entity_id: automation.tent_windy

You will require an automation that detects the cover has been opened (use the event trigger if you have no cover state) to re-enable the automation.

1 Like

EDIT: @tom_l beat me to it :slight_smile:

Have the automation turn itself off.

Have another automation turn this automation on when the cover is opened.

- id: Tent_windyy
  alias: Tent windy
  trigger:
    platform: numeric_state
    entity_id: sensor.wind_speed_automation
    above: 40
  action:
    - service: cover.close_cover
      entity_id: cover.50758014840d8e918614
      # After closing the cover, disable this automation.
    - service: homeassistant.turn_off
      entity_id: automation.tent_windy
- alias: Enable auto cover close
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: cover
        service: open
  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

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