Creating a toggle switch based on device tracker state

Greetings to everyone on this awesome community. I have been browsing the forum for quite a while and have found answers to almost every one of my questions, as a rookie in home assistant, until now.

So, here is the situation.
I have a Mikrotik router, and have the Mikrotik Router integration (the HACS one, as the official is inconsistent as hell) for presence detection.
I also have a chep generic “smart” TV, connected to the network, so the TV can be tracked whether it is ‘home’ (turned on and connected to the network) or ‘not_home’ (turned off and not connected to the network).
I have a broadlink rm4 universal remote, that I trained with basic commands to control the TV, like turning it on/off.
The goal is to create a toggle switch, that, when pressed, either direction, to activate the script that sends the power on InfraRed command to the TV, but to get its state (if its on the right or the left) based on whether the device tracker sees the TV as ‘home’ or ‘not_home’.

So far, i have created this binary sensor:

binary_sensor:
  - platform: template
    sensors:
      arielli_tv_power_status:
        friendly_name: Arielli TV Power
        icon_template: >
          {% if is_state('device_tracker.android_ef0c4c27bdbb2d2d_2', 'home') %}
            mdi:television
          {% else %}
            mdi:television-off
          {% endif %}
        
        value_template: "{{ is_state('device_tracker.android_ef0c4c27bdbb2d2d_2', 'home') }}"

The sensor gets it’s state from the device tracking of the TV.

I have created a button in lovelace that gets its state from the above binary sensor, and when pressed, runs the script to send the appropriate IR command. However its just a simple press button, with its action programmed to call the service to run the script. The icon updates based on the sensor state, but its not a toggle.

So far this works good enough, but, since I’ve been messing around quite a while, I would prefer to keep things in a more organized and elegant way. So I would like to create an entity that is a toggle switch, getting its state from the binary sensor, and when pressed, to activate the same script, regardless of the state it is in.

I hope I made it clear enough. Thank’s in advance.

The template switch is what you want:

You can use the same turn_on and turn_off actions. The important thing is the value template for the switch state. You can use the same template that you use for the binary sensor, then you could delete that if you have no other use for it.

The Lovelace switch displayed state may bounce a bit when you activate it, depending on how quickly the tracker updates. If you use a ping sensor instead you can call the update entity service as part of the turn on and turn off actions.

Here’s an example where I do that for a rest sensor that determines a switch state:

- platform: template
  switches:
    lounge_amp_adaptive_drc:
      friendly_name: Adaptive DRC
      value_template: "{{ is_state_attr('sensor.lounge_amp', 'adaptive_drc', true ) }}"
      turn_on:
      - service: rest_command.lounge_adaptive_drc_on
      - delay:
          seconds: 1
      - service: homeassistant.update_entity   #### <------ THIS
        entity_id: sensor.lounge_amp
      turn_off:
      - service: rest_command.lounge_adaptive_drc_off
      - delay:
          seconds: 1
      - service: homeassistant.update_entity
        entity_id: sensor.lounge_amp
      icon_template: "{{ 'mdi:tune-vertical'}}"

Works perfectly, exactly the way I wanted it to. Here is the code i used

switch:
    - platform: template
      switches: 
        tv_power:
            value_template: "{{ is_state('device_tracker.android_ef0c4c27bdbb2d2d_2', 'home') }}"
            turn_on:
                - service: script.arielli_tv_power
                - delay: 
                    seconds: 30
                - service: homeassistant.update_entity
                  entity_id: device_tracker.android_ef0c4c27bdbb2d2d_2
            turn_off:
                - service: script.arielli_tv_power
                - delay: 
                    seconds: 30
                - service: homeassistant.update_entity
                  entity_id: device_tracker.android_ef0c4c27bdbb2d2d_2
            icon_template: "{{ 'mdi:television' }}"

I modified the update time to keep up with the refresh rate on the device tracker of my router.
I also made a custom button card with conditional coloring according to the switch state. I paste the code here in case anyone finds it useful in the future. I used the custom-button-card frontend HACS integration.

type: horizontal-stack
title: Τηλεόραση
cards:
  - type: custom:button-card
    entity: switch.tv_power
    icon: mdi:power
    show_name: false
    show_state: false
    color: red
    state:
      - value: 'off'
        color: red
      - value: 'on'
        color: green
  - type: button
    tap_action:
      action: call-service
      service: script.arielli_tv_vol_down
      service_data: {}
      target: {}
    name: Arielli TV Vol Down
    icon: mdi:volume-medium
    show_name: false
  - type: button
    tap_action:
      action: call-service
      service: script.arielli_tv_vol_up
      service_data: {}
      target: {}
    name: Arielli TV Vol Up
    icon: mdi:volume-high
    show_name: false

EDIT: Please mark as SOLVED.
Thanks for your help

1 Like

You can mark posts for your own topics as solved (as long as they have a category). It is generally polite to mark the post that helped solve your problem though.