Why is Easy automation so hard?


#1

I’m trying to achive the automation as described below, but it’s much harder than I expected.

  • I have a room with 2 double wall switches, a PIR and a dimmer.

  • The left button of double wall switch is used to turn on the light at 100% and the right button is to turn on the light for 10%.

  • The second wall switch has the exact same function.

  • If any of the wall switch button is turned of, the light should go out

  • Next to that I have a PIR in the same room. If the PIR is triggerd after sun-set, the light should go on for 2 minutes at 10%.

I have a fresh installd RPi 3, switches, PIR and dimmer (all RF 433Mhz)) are all defined in the configuration.yaml

My automation.yaml is quite straight forward.

Problem: I can’t switch off the light using wall switch B if the light is turned on with wall switch A.
It seems to remember the state of the wall switches because in some ocations I can’t switch on the lights.

Who can help me with this puzzle.

Edwin


#2

Please share your automation so we can help. Make sure you format using the code formatting


#3

I am using Sonoff Wall switches via MQTT this should get your started with the basic switching.
Once you got that sorted you can look at dimming at 10%

What hardware are you using?


#4

automations.yaml

 - id: '552174152494'
  alias: SK licht aan 100pct
  initial_state: 'off'
  trigger:
  - entity_id: light.SK_Bed_R
    platform: state
    to: 'on'
  - entity_id: light.SK_Deur_R
    platform: state
    to: 'on'
  condition: []
  action:
    - service: light.turn_on
      entity_id: light.lamp_slaapkamer
- id: '1552174152495'
  alias: Slaapkamer 10%
  trigger:
  - entity_id: light.SK_Deur_L
    platform: state
    to: 'on'
  - entity_id: light.SK_Bed_L
    platform: state
    to: 'on'
  condition: []
  action:
    - service: light.turn_on
      entity_id: light.lamp_slaapkamer
- id: '1552174152496'
  alias: SK licht uit
  trigger:
  - platform: state
    entity_id: light.SK_Deur_R
    to: 'off'
  - platform: state
    entity_id: light.SK_Bed_R
    to: 'off'
  action:
    - service: light.turn_off
      entity_id: light.SK_Bed_R, light.SK_Deur_R, light.SK_Bed_L, light.SK_Deur_L

#5

Can you please post your code using the code format button . Instructions at the top of the page. it really helps to verify the formatting.

But a couple of things from trying to review you code

  1. It looks like you have two different automations that use the same trigger, and none of them have any conditions. Both light.sk_deur_r and light.sk_bed_l are set as triggers. One for 100% and one for 10%.
  2. You can just use light.turn_off and light.turn_on instead of homeassistant.turn_on/off. homeassistant.turn_on/off is most useful when you have a group of different types of entities, like lights and switches. that you want to turn on/off with one command.

#6

Please post the configuration for the wall switches.


#7
devices:

newkaku_00eebb56_a:
name: Lamp Slaapkamer
type: dimmable
newkaku_01b7861e_1:
name: SK-Deur L
type: switchable
newkaku_01b7861e_2:
name: SK-Deur R
type: switchable
newkaku_01b7a1da_1:
name: SK-Bed L
type: switchable
newkaku_01b7a1da_2:
name: SK-Bed R
type: switchable


#8

Please, update your automation using the proper formatting


#9

- alias: SK licht aan 100pct
  initial_state: 'off'
  trigger:
    - platform: state
      entity_id: group.slaapkamer_100pct
      to: 'on'
  action:
      service: light.turn_on
      entity_id: light.lamp_slaapkamer
- alias: SK licht uit
  initial_state: 'off'
  trigger:
    - platform: state
      entity_id: light.SK_Deur_R
      to: 'off'
    - platform: state
      entity_id: light.SK_Bed_R
      to: 'off'
  action:
    service: light.turn_off
    entity_id: group.slaapkamer100
- id: '1552174152495'
  alias: Slaapkamer 10%
  trigger:
  - entity_id: light.SK_Deur_L
    platform: state
    to: 'on'
  - entity_id: light.SK_Bed_L
    platform: state
    to: 'on'
  condition: []
  action:
    - service: light.turn_on
      data:
        entity_id: light.lamp_slaapkamer
        brightness: 10


#10

it’s not only hard, it’s also complex what you’re trying to achieve. for example: if you manually turn on the light at night and move in the room, the motion sensor will dim your lightl. so you’ll need a condition checking the state of the light (brightness = 256?). if you set a timer to turn off the light after 2 minutes (i don’t see an automation for that in your code yet), it might also turn off a manually turned on light. i also wonder how you determine wheter a switch is turned on or off? my lightswitches are either rockers that toggle the light state or push buttons. push buttons would work as dedicated on/off switches, but rockers always toggle lights. if you use push-buttons in my experience it’s better to catch push events instead of states to trigger automations.

also 10% brightness in yaml would be brightness_pct: 10.

what i would do is set up the night light automation with appropriate conditions, …

  - condition: sun
    after: sunset

… starting a script with a two minute delay. the automations to catch the switch events should not only toggle the light, but also stop the timer script, so that the timer won’t turn off the manually switched on light.


#11

So what happens if you press the a “right” switch ON. The light turns on to 100%, then you press a left switch “ON” does it turn to 10%? At that point one left switch is on, and one right switch is on, then you press the second right switch to ON, does it go back to 100%,then you press the last left switch on ON, does it go back to 10%. Now all 4 switches are ON, pressing ON for any of them should do Nothing, right? Now press any of them to OFF. The dimmable lamp should go off, but the other three switches are still “on”, that’s the issue, you want to turn off all of the switches as well, when anything is turned off

action:
service: homeassistant.turn_off
entity_id: light.lamp_slaapkamer, light.sk_deur_r, light.sk_deur_l, light.sk_bed_r, light.sk_bed_l

#12

This makes sense. I’ll try right away.


#13

Based on the configuration of the lights, it appears you are using the RFLink platform.

I’m not sure if this applies to you but there’s a section in the RFLink documentation that explains how to maintain the light’s state when an RFLink light is controlled by multiple remotes.


#14

I haven’t registered the switches and/or remotes to the lights. The wall switches trigger HA and HA sends on/of brightness to dimmer.


#15

Right,
when I press wall switch A Right ON -> light 100%
when I press wall switch A Left ON -> light 10%
when I press wall switch B Right ON -> light 100%
when I press wall switch B Left ON -> 10%

I want to use the ON state mixed in every combination and/or order.

To switch the light OFF I want to press random wall switch OFF

this is what I have now for automation.yaml

- alias: SK licht aan 100pct
  initial_state: 'off'
  trigger:
  - entity_id: light.SK_Bed_R
    platform: state
    to: 'on'
  - entity_id: light.SK_Deur_R
    platform: state
    to: 'on'
  condition: []
  action:
    - service: light.turn_on
      data:
        entity_id: light.lamp_slaapkamer
        brightness: 254
- alias: SK licht uit
  trigger:
  - platform: state
    entity_id: light.SK_Deur_R
    to: 'off'
  - platform: state
    entity_id: light.SK_Bed_R
    to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: light.SK_Bed_R, light.SK_Deur_R, light.SK_Bed_L, light.SK_Deur_L
- id: '1552174152495'
  alias: Slaapkamer 10%
  trigger:
  - entity_id: light.SK_Deur_L
    platform: state
    to: 'on'
  - entity_id: light.SK_Bed_L
    platform: state
    to: 'on'
  condition: []
  action:
    - service: light.turn_on
      data:
        entity_id: light.lamp_slaapkamer
        brightness: 10

with this automations.yaml:
light.sk_bed_l ON results in light on 10%
light.sk_bed_l OFF does not switch off light

light.sk_bed_r ON does not turn on light
light.sk_bed_r OFF does not switch off light (it was not turned on in the first place).


#16

So you have a wall switch with 4 buttons like this:
1 | 2
3 | 4

and you want to map the buttons as such:

1 = 100% brightness
2 = 10% brightness
3 = off
4 = off

watch the events in HA and look for the event that occurs when you click a button. Rewrite the automation with “platform: event” as your trigger rather than state. This way, even if your left switch is already current state off, HASS should still see some event on the switch… maybe it will come in with type = ‘click’ (xiaomi use this), or maybe a service = turn_off command can be seen.

I map my harmony and xiaomi buttons this way, even if the switch they are controlling is set to off, I still see an event for sending the turn_off command from the harmony remote, and use that event as a trigger in my automation.


#17

Yes, you are right.
This would make perfect sense.

Now I’m googleling my but off to figure out how to set this up.
Can you point me in the right direction?

  trigger:
  - platform: event
    event_type: button_pressed
    event_data: 
    entity_id: switch.sk_bed_l
  action:
    - service: light.turn_on
      entity_id: light.lamp_slaapkamer
``


2019-03-14 22:28:23 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected dict for dictionary value @ data[‘trigger’][0][‘event_data’]. Got None
extra keys not allowed @ data[‘trigger’][0][‘entity_id’]. Got ‘switch.sk_bed_l’. (See /config/automations.yaml, line 0). Please check the docs at https://home-assistant.io/components/automation/
extra keys not allowed @ data[‘trigger’][0][‘entity_id’]. Got ‘switch.sk_bed_l’. (See /config/automations.yaml, line 0). Please check the docs at https://home-assistant.io/components/automation/
extra keys not allowed @ data[‘trigger’][0][‘entity_id’]. Got ‘switch.sk_bed_l’. (See /config/automations.yaml, line 0). Please check the docs at https://home-assistant.io/components/automation/


#18

indentation issues under trigger. I don’t think the - is necessary unless you’re specifying multiple, but I’ve left it below

should be:

trigger:
  - platform: event
    event_type: button_pressed
    event_data: 
      entity_id: switch.sk_bed_l
action:
  - service: light.turn_on
    entity_id: light.lamp_slaapkamer

Looking at “button_pressed” … is there some info in HA logs showing which of the 4 buttons was pressed? that info will go in the condition clause I think


#19

Yes, I found a solution:

- id: 'Slaapkamer_10% Deur L'
  trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.sk_deur_l
      state: "on"
  action:
  - service: light.turn_on
    data:
      brightness: 10
    entity_id: light.lamp_slaapkamer
# -----------------------------------------------
- id: 'Slaapkamer_10% Bed L'
  trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.sk_bed_l
      state: "on"
  action:
  - service: light.turn_on
    data:
      brightness: 10
    entity_id: light.lamp_slaapkamer

is there a way to combine the two? I tried:

  trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.sk_bed_l, switch.sk_deur_l
 

but that didn’t work.


#20

If this doesn’t work:

- id: 'Slaapkamer_10% Deur-Bed L'
  trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.sk_deur_l, switch.sk_bed_l
      state: "on"
  action:
  - service: light.turn_on
    data:
      brightness: 10
    entity_id: light.lamp_slaapkamer

then try this:

- id: 'Slaapkamer_10% Deur-Bed L'
  trigger:
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.sk_deur_l
      state: "on"
  - platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.sk_bed_l
      state: "on"
  action:
  - service: light.turn_on
    data:
      brightness: 10
    entity_id: light.lamp_slaapkamer