Defining some variables?

Hi,

I’ve installed HA over a year ago but have only recently found time to dive deeper. I’m hoping to set up some variables as conditions for some automations, namely something like

var_someoneishome if person.me=home or person.wife=home
var_alarmactivated if person.me=home and person.wife=home and iphone=charging and time isbetween 10pm and 6am

I want to use some sort of variables so that if I change these conditions, I don’t have to go through each and every automation to make adjustments. I saw the ‘helper’ menu, but I couldn’t seem to find the correct one that allows me to define something like the above.

I’m a total beginner to HA. Any direction you can give me is appreciated. Thanks in advance.

Template Sensors and Binary Sensor

Though, the var_someoneishome example is covered by a basic state condition. The state of a zone is the number of known persons in that zone, so that can be covered by:

condition:
  - condition: numeric_state
    entity_id: zone.home
    above: 0
2 Likes
  1. Write your pseudocode as a valid template
  2. Create a template sensor or binary sensor and set your template as its state
  3. Use your template sensor around in automations. HA will keep its state up to date automatically
1 Like

Thank you @Didgeridrew and @CentralCommand .

Did not even know zone.home existed. Super cool.
Looks like I’ll dig into templates for my next project. Thank you!

Try something like this:

template:
  - binary_sensor:
    - name: Someone at home
      unique_id: someone_at_home
      device_class: occupancy
      state: "{{ (states('zone.home') | int(-1)) == 0}}"
1 Like