Create a constant

As far as the usefulness of constants, here’s one place I’d normally use a constant, and I’m curious what you guys would recommend for HA YAML.

So I have motion sensors in our bathrooms to turn on/off the lights. Cool. But when you get into the shower and close the door/curtain, the sensor no longer detects motion and turns the lights off. Now you’re showering in the dark. Not cool.

My current solution to this problem is to put a humidity sensor near the shower. Then add a condition to the “Lights Off” automation that it only activates if the humidity is below a certain value. No more unhappy wife. But… I’m still experimenting with exactly what value to use as the humidity trigger.

Especially considering I have multiple bathrooms that use this humidity value, instead of coding a literal value (i.e. magic number), I’d normally define a constant for this… but I don’t see a way to do that in HA YAML.

Example:

- id: 'spam'
  alias: Motion Lights Off - Master Bath
  trigger:
  - entity_id: binary_sensor.master_bath_motion_sensor_motion
    for: 00:02:00
    platform: state
    to: 'off'
  condition:
  - below: '65'
    condition: numeric_state
    entity_id: sensor.master_bath_humidity_sensor_humidity
  action:
  - data:
      transition: 5
    entity_id: light.master_bath_lights
    service: light.turn_off

In the above example, I’d love to be able to define a constant for the “65” and maybe even for the “5” on the transition to make tweaking things easier when those values are used in multiple automations. Suggestions?

Use an input_number.

Thanks! That seems to work well. From reading about how to use input_number , it sounds like you can’t just put that in the below or above field; using a template seems to be the way to do it, right?

Example:

  condition:
  - condition: template
    value_template: '{{ states("sensor.master_bath_humidity_sensor_humidity")  | float 
      < states("input_number.humidity_threshold_shower") | float }}'
1 Like

Yeah, that’s how to do it :+1:

Hi,
I suggets to deine a binary_sensor and use the sensor state as condition.

Example:

  • platform: template
    sensors:
    humidity:
    friendly_name: “BathHumidityLightLevel”
    device_class: none
    value_template: “{{states(‘sensor.bath1_humidity’)|float>60
    or states(‘sensor.bath2_humidity’)|float>60}}”

Can i also update that static number through automation?

Thanks exactly what I was looking for!

Simple Const idea

input_text:
  my_const:
  initial: 'required_value'

I’ve read through this chain and checked out a few others but remain unable to find a solution to my goal of using constants in Lovelace cards, As an example here is a Gauge card for rainfall:

type: gauge
min: 0
entity: sensor.rainfall_month
name: Month
needle: true
severity:
  green: 2.5
  yellow: 4.5
  red: 6
max: 8

This gauge is repeated a couple of times in other places so if I decide that I want to change the min/max or severity values I need to edit each instance. I’ve tried the suggestions described earlier but either they don’t address this instance or I am not implementing them properly. Any thoughts are appreciated.
Ron