Automation based on TV and PIR

Trying to achieve the following:

  • if TV is ON and PIR detects movement turn on a light in dim mode (and keep it on)
  • if TV is OFF and PIR detects movement turn on a light in bright mode

of course if no PIR movement then lights stays off

Possible, how?

Thanks

Assuming you have some entity that can tell you if tv is on or off yes pretty easy to implement. Just using automations and a template for changing the brightness setting after light.turn_on is called.

trigger will be the PIR sensor detecting motion, action will be call the light.turn_on service.

action:
  service: light.turn_on
  data_template:
    entity_id: light.lightname
    brightness: '{% if is_state("entityid.tv", "on") %}255{% else %}100'

Something like that… its been awhile since ive looked at my home assistant configs so might be some errors in syntax there.

I should exchange 255 with 100, right? I mean if TV on 100, if off 255. Yes?

Yep you’re right, sorry.

trying now

17-04-18 22:50:03 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘endif’. The innermost block that needs to be closed is ‘if’.) for dictionary value @ data[‘action’][0][‘data_template’][‘brightness’]. Got None. (See /home/hass/.homeassistant/configuration.yaml, line 1278). Please check the docs at Automation - Home Assistant

some brackets to be changed?
I have no clue

Oh yep you need {% endif %} at the end, use the template editor from the dashboard to debug the template until it works.

like this?

brightness: ‘{% if is_state(‘media_player.yamaha_receiver’, ‘on’) %}120{% else %}255, {% endif %}’

(a comma after 255?)

trying now, result is

brightness: '255, ’

is the , and space correct to have?

no, I got it

brightness: ‘{% if is_state(‘media_player.yamaha_receiver’, ‘on’) %}120{% else %}255{% endif %}’

this

action:
  service: light.turn_on
  data_template:
    entity_id: light.tv_standing_lamp
    brightness: '{% if is_state('media_player.yamaha_receiver', 'on') %}120{% else %}255{% endif %}'

gives this error

17-04-18 23:14:52 ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
  in "/home/hass/.homeassistant/automation.yaml", line 580, column 9
expected <block end>, but found '<scalar>'
  in "/home/hass/.homeassistant/automation.yaml", line 581, column 38
17-04-18 23:14:52 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/hass/.homeassistant/configuration.yaml: while parsing a block mapping
  in "/home/hass/.homeassistant/automation.yaml", line 580, column 9
expected <block end>, but found '<scalar>'
  in "/home/hass/.homeassistant/automation.yaml", line 581, column 38

You must use double quotes around media_player.yamaha_receiver and on in the template

1 Like

ok, done that.

It works!

Thanks