Help with logic & syntaxing for a light toggle

Hi all,

Hoping for some guidence with logic.

I have a bedside light and a kodi instance

I have a flic button that I have been playing with which does the follwoing:

Single Click = light.toggle
DoubleClick = light.toggle & media_player.media_play_pause

So on a double click kodi pauses and turns my light on, I pop off and do someting, come back, double click for the light to turn off and kodi to resume.

  • alias: Pause & Toggle Bedside
    trigger:
    platform: event
    event_type: flic_click
    event_data:
    button_name: flic_80e4da723c3a
    click_type: double
    action:
    • service: media_player.media_play_pause
      entity_id: media_player.kodi__atv
    • service: light.toggle
      entity_id: light.bedside

This all works well until either the light is already on or kodi is already paused. Then things go out of sync so Im looking to put a bit of logic around the toggle.

I’ve been working through some things but I just cant seem to get it.

The last bit I tried was:

  event_data:
    button_name: flic_80e4da723c3a
    click_type: double
action:
- service: media_player.media_play_pause
  entity_id: media_player.kodi__atv
- service_template: light.{%- if is_state('media_player.kodi__atv', 'paused') -%}turn_on{%- elif is_state('media_player.kodi__atv', 'playing') -%}turn_off{%- else -%}turn_off{%- endif -%}
  entity_id: light.bedside

Basically using kodis state and testing against the curent light state to decide what action to take. This way, no matter what state either is in, they wont get out of sync.

Im struggling with a) the syntaxing and b) if this is actually the right way to go about it.

Hoping someone would be able to advise?

Have you tried the template editor to see if your service template syntax is providing you the expected results?

Sorry, had to shelve this for a bit as had an influx of new devices to add and setup :slight_smile:

Taking another look at this now.

So, in the template editor:

light.{%- if is_state('media_player.kodi__atv', 'paused') -%}turn_on{%- elif is_state('media_player.kodi__atv', 'playing') -%}turn_off{%- else -%}turn_off{%- endif -%}

Results:

Kodi playing: light.turn_off
Kodi paused: light.turn_on

The first part of the logic seems to be working but it still goes wonky.

If I pause kodi and turn the light on, the result goes back to: light.turn_on
If I then play kodi the result changes to: light.turn_off
If I then pause kodi the result changes to: light.turn_on - Getting things out of sync again.

I feel like I need another bit of logic in there somewhere to test against the state of the light at the time the button is pressed and match against the state of kodi but I have no idea how to do that!

What you might do is make the light or Kodi master and the other slave. Then if you double click, toggle master and sync slave to that state.

Aaaa so something Along the lines of: (butchered from How to Do on / OFF group switch)

state = hass.states.get(data['master']).state

if state == 'paused':
    hass.services.call('homeassistant', 'turn_off',
                       {'entity_id': data['slave']})
else if state == 'playing':
    hass.services.call('homeassistant', 'turn_on', {
                       'entity_id': data['slave']})


trigger:
      platform: event
      event_type: flic_click
      event_data:
        button_name: flic_80e4da723c3a
        click_type: double
    service: python_script.kodi_toggle
    data:
      master: mediaplayer.kodi
      slave: light.bedside

This earlier example of yours looks like the right way to do this.

My approach:

automation:
  - alias: Double Click Flic
    trigger:
      event_data:
        button_name: flic_80e4da723c3a
        click_type: double
    action:
      # toggle master
      - service: media_player.media_play_pause
        entity_id: media_player.kodi__atv
      # ---> if needed, build in a delay here
      # sync slave
      # ... syncs playing state with lights on. you can also revert and sync with state 'paused'
      - service_template: >
        {% if is_state('media_player.kodi__atv', 'playing') %}
          light.turn_on
        {% else %}
          light.turn_off
        {% endif %}
        entity_id: light.bedside
1 Like

Thats just wonderful mate!

When I first started, I got close to something like this but with multiple if/elses for each condition which worked but really wonky and about 90 lines! Nothing as fluid as this.

Just tested and it works perfectly :smiley:

1 Like