Button state based on sensor value

Hello,

Hopefully the titel says it al. Is it possible to have a button state change depending on the value of a sensor? I don’t want to toggle the button, just change the state of the icon.

The reason I want this is the following:

I have a media/gaming PC in my home with a relay hardwired in the powersuply, connected to the reset/power pins on the motherboard. With this I can safely turn on/off the computer without cutting of the power and maybe demaging internal components.

The computer powersuply itself is connected via a smart socket measuring the current powerusage. Therefor I ‘know’ when my PC is turned on/off and also when it’s Idle (depending on the watts using).

I have a wallmounted tablet with a button that says ‘Computer’. On pushing the button, it toggles the relais starting or shutting down the PC.

Currently I disabled the buttonstate function so its always shown as ‘off’. Rather have it so its visible that its already on based on the sensors wattsusage.

Love to hear if its possible or maybe some other way to create the desired result.

Cheers,
Seyude

Create a template switch:

The on/off services are toggling your relay, the value template for the state will be a template that checks the power level is above a threshold.

Omg… so simple. Thanks for that.

1 Like

Can you share more details about that ?

Sure, what do you want to know?

What relay, how did you connect it ?

Aaa, okay.

I used the Fibaro relay FGS-212. I solderd the main voltage input of the relay to the internal 230v pins of my PC powersuply unit. (Just open it and search for the backside of the powerplug).

The relay wires can be any voltage, or none. The latest is used in this setup. They are connected to the 2pins on the motherboard were you can connect a reset button to. Important is to first make sure the wires have no voltage, because they can also output 230v so you can fry your motherboard… Giving a ‘turn_on’ command turns the PC on when off and off when on. I think you also need to go into your windows or bios settings to set how your PC reacts to the pressing of the power/reset button. You can also tell your PC to reboot for instance.

Now you have a Z-wave connected PC powerbutton.

The secondary powermeasure unit is used to measure the PC usage. When the PC is off, it uses around 3 w. When it turns on it goes up to around 120 w. Idle is around 60. This way you can make a variable with 3 states saying whats the state of the pc.

Thanks, I appreciate it.

Helpfull in my case since my PC is rackmounted in a seperate room. When I use my logitec remote to select the use of my PC on the livingroom TV it now also turns on my PC if needed.

And turns off safely the PC when going away or go to sleep before turning off the power in the rack for several devices. A extra UI button helps me to prevent the shutdown of the PC in case of ongoing tasks like rendering video or downloading big files.

1 Like

I’m using a current sensor and notice the TV consistently uses above 20W of power when on. This is only needed because my TV the gets toggled on and off using a remote and via an IR blaster connected to HA and frequently the status of my TV in HA is incorrect stemming from the use of the remote vs the IR blaster.

I tried the above, and while the Developer tab shows my sensor status correctly and I’m not getting errors, my switch’s status isn’t tracking with my sensor. Here’s my code:

My switch:

mqtt:
  switch:
    - name: "LG TV FR"
      value_template: "{{ is_state_attr('switch.lg_tv_fr', states('sensor.lg_tv_fr_status'), 'on') }}"
      command_topic: "cmnd/tasmota_1C1CE3/IRsend"
      payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'
      payload_off: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'

My sensor:

      lg_tv_fr_status:
        friendly_name: "LG TV FR Status"
        value_template: "{{ 'on' if states('sensor.lg_power_plug_current_consumption') | float > 20 else 'off' }}"

My button:

cards:
  - show_name: true
    show_icon: true
    type: button
    entity: switch.lg_tv_fr
    show_state: true

Any suggestions?

Solution: @Taras had a really creative solution: Combine MQTT Switch and Binary Sensor - #10 by 123

In English, an automation fires when the detected wattage from my sensor exceeds 20W or falls below 5W. That automation sends out a MQTT message “ON” if the wattage exceeds 20W, or “OFF” of the wattage falls below 5W. The MQTT switch listens on the state_topic for the MQTT message and compares the received value to my state_on and state_off conditions. As expected, the button action triggers the my IR device to turn the TV on and off, and the status of the TV is reported based on the value from my Kasa smart plug.

My switch in my switches.yaml file:

mqtt:
  switch:
    - name: "LG_TV_FR" #the entity name
      state_topic: 'cmnd/tasmota_1C1CE3/lg'
      state_on: 'ON'
      state_off: 'OFF'
      command_topic: "cmnd/tasmota_1C1CE3/IRsend"
      payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'
      payload_off: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'

An automation:

alias: MQTT_LG_TV
description: ""
triggers:
  - entity_id:
      - sensor.lg_power_plug_current_consumption
    below: 5
    trigger: numeric_state
  - entity_id:
      - sensor.lg_power_plug_current_consumption
    trigger: numeric_state
    above: 20
conditions: []
actions:
  - choose:
      - conditions:
          - type: is_power
            condition: device
            device_id: 4e76f6a8511344758cca72e5dc232009
            entity_id: 6e04531b3c64bd430164e2f0be0592a6
            domain: sensor
            below: 5
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              evaluate_payload: false
              qos: 0
              retain: true
              topic: cmnd/tasmota_1C1CE3/lg
              payload: "OFF"
      - conditions:
          - type: is_power
            condition: device
            device_id: 4e76f6a8511344758cca72e5dc232009
            entity_id: 6e04531b3c64bd430164e2f0be0592a6
            domain: sensor
            above: 20
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              evaluate_payload: false
              qos: 0
              retain: true
              topic: cmnd/tasmota_1C1CE3/lg
              payload: "ON"
mode: single
hide_entity: true

And my button:

type: horizontal-stack
cards:
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.lg_tv_fr
    show_state: true
title: LG TV Family Room