Mistake in automation?

Hi there,

I’m on my first steps with Home Assistant and I would like to start with automations.
The automation-tool looks a bit strange to me, so I tried it in the automation.yaml.

What do I want to do?
If the entity sensor.ipad_kuche_battery_level is below 35% AND switch.netzteil_tablets_schalten is off, I would like to switch it on.
When sensor.ipad_kuche_battery_level is above 79% AND switch.netzteil_tablets_schalten is on, I would like to switch it off.

So this is the content of my automation.yaml:

automation: #Netzteil für Tablet laden einschalten
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.ipad_kuche_battery_level
    - platform: state
      entity_id:
        - switch.netzteil_tablets_schalten
      state: 'off'
  action:
    - service: switch.turn_on
      target:
       entity_id: switch.netzteil_tablets_schalten
      
      
automation2: #Netzteil für Tablets laden abschalten 
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.ipad_kuche_battery_level
      above: 79
    - platform: state
      entity_id:
        - switch.netzteil_tablets_schalten
      state: 'on'
  action:
    - service: switch.turn_off
      target:
       entity_id: switch.netzteil_tablets_schalten

But it seems, I did anything wrong…

Anyone wo sees the mistake?
Ans is it correct, that the automation isn’t visible in the automation-tool?

Cheers!

Doesn´t either of the automations work? The second looks correct, but in the first automation you miss the below 35% statement, so that won´t work for sure.

Triggers are always OR.
Use the switch as trigger and the sensor as condition (or reverse).

Ok, so here is the new one:

automation: #Netzteil für Tablet laden einschalten
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.ipad_kuche_battery_level
      below: 35
  condition:
    - platform: state
      entity_id:
        - switch.netzteil_tablets_schalten
      state: 'off'
  action:
    - service: switch.turn_on
      target:
       entity_id: switch.netzteil_tablets_schalten
      
      
automation2: #Netzteil für Tablets laden abschalten 
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.ipad_kuche_battery_level
      above: 79
  condition:
    - platform: state
      entity_id:
        - switch.netzteil_tablets_schalten
      state: 'on'
  action:
    - service: switch.turn_off
      target:
       entity_id: switch.netzteil_tablets_schalten

And the Failurelog:

Logger: homeassistant.components.automation
Source: components/automation/config.py:207
Integration: Automatisierung (documentation, issues)
First occurred: 12:25:01 (2 occurrences)
Last logged: 12:25:02

Unnamed automation could not be validated and has been disabled: extra keys not allowed @ data[‘automation’]. Got OrderedDict([(‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, [‘sensor.ipad_kuche_battery_level’]), (‘below’, 35)])]), (‘condition’, [OrderedDict([(‘platform’, ‘state’), (‘entity_id’, [‘switch.netzteil_tablets_schalten’]), (‘state’, ‘off’)])]), (‘action’, [OrderedDict([(‘service’, ‘switch.turn_on’), (‘target’, OrderedDict([(‘entity_id’, ‘switch.netzteil_tablets_schalten’)]))])])]) extra keys not allowed @ data[‘automation2’]. Got OrderedDict([(‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, [‘sensor.ipad_kuche_battery_level’]), (‘above’, 79)])]), (‘condition’, [OrderedDict([(‘platform’, ‘state’), (‘entity_id’, [‘switch.netzteil_tablets_schalten’]), (‘state’, ‘on’)])]), (‘action’, [OrderedDict([(‘service’, ‘switch.turn_off’), (‘target’, OrderedDict([(‘entity_id’, ‘switch.netzteil_tablets_schalten’)]))])])]) required key not provided @ data[‘action’]. Got None required key not provided @ data[‘trigger’]. Got None

The automations have no id and no alias.

Ok… so what do I have to edit?

I took this as template and it looks fine to me:

Is this better?

automation: #Netzteil für Tablet laden einschalten
 - alias: "Netzteil Visu-Tablet ein"
   trigger:
     - platform: numeric_state
       entity_id:
         - sensor.ipad_kuche_battery_level
       below: 35
   condition:
     - platform: state
       entity_id:
         - switch.netzteil_tablets_schalten
       state: 'off'
   action:
     - service: switch.turn_on
       target:
        entity_id: switch.netzteil_tablets_schalten
      
      
automation2: #Netzteil für Tablets laden abschalten 
 - alias: "Netzteil Visu-Tablet aus"
   trigger:
     - platform: numeric_state
       entity_id:
         - sensor.ipad_kuche_battery_level
       above: 79
   condition:
     - platform: state
       entity_id:
         - switch.netzteil_tablets_schalten
       state: 'on'
   action:
     - service: switch.turn_off
       target:
        entity_id: switch.netzteil_tablets_schalten

Failure is

Unnamed automation could not be validated and has been disabled: extra keys not allowed @ data[‘automation’]. Got OrderedDict([(‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, [‘sensor.ipad_kuche_battery_level’]), (‘below’, 35)])]), (‘condition’, [OrderedDict([(‘platform’, ‘state’), (‘entity_id’, [‘switch.netzteil_tablets_schalten’]), (‘state’, ‘off’)])]), (‘action’, [OrderedDict([(‘service’, ‘switch.turn_on’), (‘target’, OrderedDict([(‘entity_id’, ‘switch.netzteil_tablets_schalten’)]))])])]) extra keys not allowed @ data[‘automation2’]. Got OrderedDict([(‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, [‘sensor.ipad_kuche_battery_level’]), (‘above’, 79)])]), (‘condition’, [OrderedDict([(‘platform’, ‘state’), (‘entity_id’, [‘switch.netzteil_tablets_schalten’]), (‘state’, ‘on’)])]), (‘action’, [OrderedDict([(‘service’, ‘switch.turn_off’), (‘target’, OrderedDict([(‘entity_id’, ‘switch.netzteil_tablets_schalten’)]))])])]) required key not provided @ data[‘action’]. Got None required key not provided @ data[‘trigger’]. Got None
Unnamed automation could not be validated and has been disabled: extra keys not allowed @ data[‘automation’]. Got [OrderedDict([(‘alias’, ‘Netzteil Visu-Tablet ein’), (‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, [‘sensor.ipad_kuche_battery_level’]), (‘below’, 35)])]), (‘condition’, [OrderedDict([(‘platform’, ‘state’), (‘entity_id’, [‘switch.netzteil_tablets_schalten’]), (‘state’, ‘off’)])]), (‘action’, [OrderedDict([(‘service’, ‘switch.turn_on’), (‘target’, OrderedDict([(‘entity_id’, ‘switch.netzteil_tablets_schalten’)]))])])])] extra keys not allowed @ data[‘automation2’]. Got [OrderedDict([(‘alias’, ‘Netzteil Visu-Tablet aus’), (‘trigger’, [OrderedDict([(‘platform’, ‘numeric_state’), (‘entity_id’, [‘sensor.ipad_kuche_battery_level’]), (‘above’, 79)])]), (‘condition’, [OrderedDict([(‘platform’, ‘state’), (‘entity_id’, [‘switch.netzteil_tablets_schalten’]), (‘state’, ‘on’)])]), (‘action’, [OrderedDict([(‘service’, ‘switch.turn_off’), (‘target’, OrderedDict([(‘entity_id’, ‘switch.netzteil_tablets_schalten’)]))])])])] required key not provided @ data[‘action’]. Got None required key not provided @ data[‘trigger’]. Got None

If you mean the default automations.yaml (that’s the file all UI automations are stored), I can only strongly recommend to create a simple test automation via UI editor and after that to check the automations.yaml file. It looks as follows:


- id: '1668811850554'
  alias: Test
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.adventszeit
# (and so on)

It is a big list that directly starts with the first entry (no automation: in first line).

Ok I did it with the Automation-plugin and now there is no failure - but it still doesn‘t Work / doesn‘t Switch

What is wrong Here?

alias: Netzteil Tablets laden einschalten
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    below: 50
condition:
  - condition: state
    entity_id: switch.netzteil_tablets_schalten
    state: "off"
action:
  - condition: state
    entity_id: switch.netzteil_tablets_schalten
    state: "on"
mode: single

Was soll passieren?
Wenn der Ladezustand unter 50% sinkt UND das Netzteil aus ist, dann soll es eingeschaltet werden.

In the automation’s action you used a condition instead of an action. If you want the automation to turn off the switch, use an action like a service call to do it.

alias: Netzteil Tablets laden einschalten
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    below: 50
condition:
  - condition: state
    entity_id: switch.netzteil_tablets_schalten
    state: "off"
action:
  - service: switch.turn_on
    target:
      entity_id: switch.netzteil_tablets_schalten
mode: single

Be advised that this automation will trigger only at the moment the battery level decreases below 50. If the battery level is already below 50, it won’t trigger the automation. It’s the act of crossing the threshold of 50 that serves to trigger it

Oh thats an important Information! Thank you!
How can I ensure, that it also works, when the level is already below 50?

Thank you for your information about the condition/Action-thing!!

Add triggers for HA restart and automation reload, and a condition to check the level.

alias: Netzteil Tablets laden einschalten
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    below: 50
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition:
  - condition: state
    entity_id: switch.netzteil_tablets_schalten
    state: "off"
  - condition: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    below: 50
action:
  - service: switch.turn_on
    target:
      entity_id: switch.netzteil_tablets_schalten
mode: single

My very similar automation that also turns the charger off again:

- alias: Misc - kitchen display charge manager

  description: >-
    Maintain kitchen display battery SoC between 35 and 80%.
    Operates by switching the plug running the
    USB charger.

  id: 6cef7c4e-2074-4406-8bf1-4f563247e9f2

  trigger:
    - platform: numeric_state
      entity_id: sensor.kitchen_display_battery_level
      above: '80'
      for:
        minutes: 1
    - platform: numeric_state
      entity_id: sensor.kitchen_display_battery_level
      below: '35'
      for:
        minutes: 1
    - platform: homeassistant
      event: start
    - platform: event
      event_type: automation_reloaded

  action:
    - choose:
      - conditions: "{{ states('sensor.kitchen_display_battery_level')|int(0) > 80 }}"
        sequence:
          - service: switch.turn_off
            entity_id: switch.sinilink_usb_switch
      - conditions: "{{ states('sensor.kitchen_display_battery_level')|int(0) < 35 }}"
        sequence:
          - service: switch.turn_on
            entity_id: switch.sinilink_usb_switch
1 Like

I know this is solved but to address the original issue…

the problem was that you had the “automation:” header in the config.

if you removed those I believe it should have worked as expected:

- alias: "Netzteil Visu-Tablet ein"
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.ipad_kuche_battery_level
      below: 35
  condition:
    - platform: state
      entity_id:
        - switch.netzteil_tablets_schalten
      state: 'off'
  action:
    - service: switch.turn_on
      target:
        entity_id: switch.netzteil_tablets_schalten
     
- alias: "Netzteil Visu-Tablet aus"
  trigger:
    - platform: numeric_state
      entity_id:
        - sensor.ipad_kuche_battery_level
      above: 79
  condition:
    - platform: state
      entity_id:
        - switch.netzteil_tablets_schalten
      state: 'on'
  action:
    - service: switch.turn_off
      target:
        entity_id: switch.netzteil_tablets_schalten
1 Like

Thank you very much for your help.
Learned a lot!

So - this is the solution, how I made it in the end:

alias: Netzteil Tablets laden einschalten
description: ""
trigger:
  - platform: time_pattern
    minutes: /3
condition:
  - condition: state
    entity_id: switch.netzteil_tablets_schalten
    state: "off"
  - condition: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    below: 30
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.netzteil_tablets_schalten
mode: single

If I understood it right, this checks every three minutes, if the switch is off AND the battery level is below 30. Both have to be true at the same time - then the switch will be switched on.

Same with the “off”-function. Only differences:

  • on and off are exchanged
  • “above 79” instead of “below 30”

Cheers!

It’s typically a bad idea to use a time pattern trigger in an automation. It can usually be solved in better ways that doesn’t require the system to “poll” the time pattern and trigger the automation before checking the conditions.

your original automations were the best solution since you only really care what the state of the battery is. So that should be the trigger.

The only “downside” is that you need two automations. I use quotes because having two automations isn’t as much of a big deal as many seem to believe. And it makes the automations much simpler because you don’t end up needing to use templates, choose, if, etc in the actions.

But even in your automation above you will still need another automation to turn the switch off again.

all that said, if I were you, I would use your two original automations and eliminate the time pattern trigger “polling” method.

The following single automation turns the tablet on/off depending on the battery level and the tablet’s current state. It doesn’t use a Time Pattern Trigger; it triggers only when needed.

alias: example 
description: ""
trigger:
  - id: 'on'
    platform: state
    entity_id: switch.netzteil_tablets_schalten
    to: 'off'
    from: 'on'
    variables:
      is_true: "{{ states('sensor.ipad_kuche_battery_level') | float(0) < 30 }}"
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    below: 30
    variables:
      is_true: "{{ is_state('switch.netzteil_tablets_schalten', 'off') }}"
  - id: 'off'
    platform: state
    entity_id: switch.netzteil_tablets_schalten
    to: 'on'
    from: 'off'
    variables:
      is_true: "{{ states('sensor.ipad_kuche_battery_level') | float(0) > 79 }}"
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.ipad_kuche_battery_level
    above: 79
    variables:
      is_true: "{{ is_state('switch.netzteil_tablets_schalten', 'on') }}"
condition:
  - condition: template
    value_template: "{{ is_true }}"
action:
  - service: 'switch.turn_{{ trigger.id }}'
    data: {}
    target:
      entity_id: switch.netzteil_tablets_schalten
mode: single
1 Like

Thank you very much!
This looks perfect to me and learned a lot with it! In the end - this is the automation I’m using now :wink:

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.