Automation :: Action :: Using Sensor state value

Hi,

In my config, I already have “virtual” sensor that calculates outdoor sunlight “percentage” (Got it from the forum). It works fine.

Now I want to use this sensor to set the kitchen light brightness to the same level of outdoor sunlight. I search forum, internet and didn’t find the answer to my problem.

My code looks like this in my automations.yaml file :

- id: '1600635450840'
  alias: 'Lumières cuisine :: Auto luminosité'
  description: ''
  trigger:
  - platform: device
    type: turned_on
    device_id: 82e8a2e8ce894c6ba6c70d8e2a7fe47e
    entity_id: light.lumieres_cuisine
    domain: light
  condition: []
  action:
  - type: turn_on
    device_id: 82e8a2e8ce894c6ba6c70d8e2a7fe47e
    entity_id: light.lumieres_cuisine
    domain: light
    data_template:
      brightness_pct: >
        {{ sensor.sunlight_pct.state|float }}
  mode: single

When I try to reload the Automations, I get this error in the log file. Looks like I’m not giving a float value or something. As you can see, I tried the “|float” but no luck.

Logger: homeassistant.config
Source: config.py:413
First occurred: 17:01:31 (3 occurrences)
Last logged: 17:07:01

Invalid config for [automation]: expected float for dictionary value @ data['brightness_pct']. Got None. (See /config/configuration.yaml, line 94).
Invalid config for [automation]: [data_template] is an invalid option for [automation]. Check: automation->data_template. (See /config/configuration.yaml, line 94).

Thanks in advance from this n00b ! :smiley:

I don’t think you can use templates with ‘device’ actions, you need to use the light.turn_on service

- id: '1600635450840'
  alias: 'Lumières cuisine :: Auto luminosité'
  description: ''
  trigger:
  - platform: device
    type: turned_on
    device_id: 82e8a2e8ce894c6ba6c70d8e2a7fe47e
    entity_id: light.lumieres_cuisine
    domain: light
  condition: []
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.lumieres_cuisine 
      brightness_pct: "{{ sensor.sunlight_pct.state|float }}"

(note that if you’re on the latest version you can use data: not data_template: if you prefer)