Dynamically show/hide a sensor

I have a group that shows my family devices and their travel times to home using the google travel time sensor. What I want to do is hide the travel time sensors if that person is home.

My first thought was to put each travel time sensor into its own group and then use automations to use group.set_visibility, however, because the travel time sensor is in a group, the group state always shows “unknown” when it’s showing. Clicking the group shows the sensor info inside and it has the correct state. I’ve been trying to figure out how to get the group state to mirror the state of the single sensor inside, but not sure how to do that.

My second thought was to just set the hidden state on the sensor (like you can do in customize) with an automation. However, I can’t figure out how to set the state of a sensor with an automation either.

Any ideas how to achieve the behaviour I’m going for?

2 Likes

What I have done in my setup is to create two groups per person: one I show when the person is home (contains the device tracker itself + the phones battery percentage) and a second group that contains the same sensors + the travel times. In my automations I have two automations per person, one that is triggered when the person comes home and one when the person leaves and it then changes the groups visibility.
Is that what you are trying to achieve?

The indentation of the yaml below might not be correct, I am not that familiar with how to copy code in this forum.

group:      
   tobi_not_home:
      name: Tobi
      entities:
          - device_tracker.homeassistant_tobi
          - sensor.tobi_phone_battery
          - sensor.time_to_drive_home_tobi
          - sensor.time_to_home_on_public_transport_tobi
          - sensor.time_to_bike_home_tobi
          - sensor.time_to_walk_home_tobi
   tobi_home:
      name: Tobi
      entities:
          - device_tracker.homeassistant_tobi
          - sensor.tobi_phone_battery

automation:
   - id: tobi_not_home
     alias: Tobi not home
     trigger:
       - platform: state
         entity_id: group.tobi_devices
         from: 'home'
         to: 'not_home'
     action:
       - service: group.set_visibility
         entity_id: group.tobi_not_home
         data:
             visible: True
       - service: group.set_visibility
         entity_id: group.tobi_home
         data:
            visible: False
  - id: tobi_home
    alias: Tobi home
    trigger:
       - platform: state
         entity_id: group.tobi_devices
         from: 'not_home'
         to: 'home'
       - platform: homeassistant
         event: start
     action:
       - service: group.set_visibility
         entity_id: group.tobi_not_home
         data:
            visible: False
       - service: group.set_visibility
         entity_id: group.tobi_home
         data:
            visible: True
2 Likes

Awesome, that approach worked for me! I made a couple changes though. First, I added a condition on the device state since otherwise I think the homeassistant start event will trigger both of these. Second, a minor simplification you can make is that you only need either to: or from: in the triggers, not both. I opted to just use to: 'home' for the home automation and from: 'home' for the not_home automation.

The device state condition is a great idea. I’ll probably incorporate this. To avoid both groups being shown after a homeassistant restart I currently have the “homeassistant - event start” trigger in the “home” automation as I am home 99% of the times when I restart homeassistant. This automation hides the away group and only shows the home group. However, having a device state condition is certainly better.
I’m glad this worked for you too!

In my case I’m tracking more than one person, so there’s a bigger chance someone else will be away while i’m at home restarting HA, hence the device state condition.

Thank you for this example. I was just trying to figure out how to hide my in bed presence sensors when my wife or I get out of bed. Based on your example I setup four copies of the group they appear in and here’s how I handled the on/off automations.

Sorry to revive and old thread, I just thought I’d share my example for the next person that finds this thread. :slight_smile:

    - alias: Hide empty bed states
    trigger:
      - platform: state
        entity_id:
          - sensors.brians_in_bed
          - sensors.wife_in_bed
        to: 'off'
    action:
      - service: group.set_visibility
        data_template:
          entity_id: "group.panel_control_{{ trigger.entity_id.split('.')[1] }}"
          visible: False
      - service: group.set_visibility
        data_template:
          entity_id: >
            {%- if  states('binary_sensor.brians_in_bed')  == 'off'
                and states('binary_sensor.wife_in_bed') == 'off' -%}
              group.panel_control
            {%- elif states('binary_sensor.brians_in_bed')  == 'on' -%}
              group.panel_control_brians_in_bed
            {%- elif states('binary_sensor.wife_in_bed')  == 'on' -%}
              group.panel_control_wife_in_bed
            {%- endif -%}
          visible: True

  - alias: Reveal bed presence
    trigger:
      - platform: state
        entity_id:
          - sensors.brians_in_bed
          - sensors.wife_in_bed
        to: 'on'
    action:
      - service: group.set_visibility
        data_template:
          entity_id: >
            {%- if  states('binary_sensor.brians_in_bed')  == 'on'
                and states('binary_sensor.wife_in_bed') == 'on' -%}
              group.panel_control_in_bed
            {%- elif states('binary_sensor.brians_in_bed')  == 'on' -%}
              group.panel_control_brians_in_bed
            {%- elif states('binary_sensor.wife_in_bed')  == 'on' -%}
              group.panel_control_wife_in_bed
            {%- endif -%}
          visible: True

How did you add the group to the dashboard? I tried but it just shows the name of the group. I have to click it in order to see the underlying sensors…