Binary sensor don´t change state from value_template

I had this running for a couple of days now, but it does not change the value of the binary_sensor.

sensor.pir_utetrapp_luminance is truely reporting luminance back in lux, as expected, and it updates regularely.

What I want is the sensor to change state (from false to true) when the luminance is below 50 lux. Obviously it has been darker than 50 lux last night.

binary_sensor:
  - platform: template
    sensors:
      dark_outside:   ### Set the sensor to true if luminance is below 50 at exerior stairs
        entity_id: sensor.pir_utetrapp_luminance
        friendly_name: Mørkt utendørs
        value_template: >-
          {{ states('sensor.pir_utetrapp_luminance')|float < 50 }}

That looks ok to me, assuming that first line only appears once in your config, and that the entity_id is correct.

Have you restarted home assistant since defining the sensor?

Yes :thinking:

This binary_sensor is within a package. I do have a /config/binary_sensor.yaml, which is empty too…

Just for fun, paste the template into the Template Editor.

  • Cover sensor.pir_utetrapp_luminance so its luminance value falls below 50. Refresh the Template Editor and confirm it reports True.
  • Uncover the sensor, refresh the editor, confirm it reports False.

Ok as long as that only appears once in the package.

Did you include the package?

I sure did.

This works too…
Maybe there were som hick-up last night. I wait another 24h to see what happends here…

Both these triggered as well, pushing notification to my phone.

  - alias: '[House] Det er mørkt ute'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: binary_sensor.dark_outside
      to: 'on'
    action:
      - service: notify.adults
        data:
          title: 'Home Assistant'
          message: 'Det er mørkt ute, kl. {{now().strftime("%H:%M")}}'


  - alias: '[House] Det er lyst ute'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: binary_sensor.dark_outside
      to: 'off'
    action:
      - service: notify.adults
        data:
          title: 'Home Assistant'
          message: 'Det er lyst ute, kl. {{now().strftime("%H:%M")}}'

What does the binary_sensor’s history show for last night?

I know this might not help your original issue, but using a template without using data_template: still feels incorrect (though using the notify service, this seems to be ‘overlooked’…)

use:

    action:
      - service: notify.adults
        data_template:
          title: 'Home Assistant'
          message: 'Det er lyst ute, kl. {{now().strftime("%H:%M")}}'
2 Likes

The binary_sensor.dark_outside reported off for the last 24 hours. Av is Off

value_template: >-
  {{ states('sensor.pir_utetrapp_luminance')|float < 50 }}

The binary_sensor.dark_inside reported on between 20:36 and 06:52, which seems reasonable is On

value_template: >-
  {{ states('sensor.pir_utetrapp_luminance')|float < 150 }}

You´re right. Thank you for your heads-up :slight_smile:

The two dark sensors uses different thresholds for reporting darkness (50 and 150).

The one using a threshold of 150 reports correctly but the one using 50 does not. Can you confirm that the luminance value really does fall below 50 during the night? The scale of the graph you posted makes it difficult to see that.

And for the record: Here are the two sensors:


binary_sensor:
  - platform: template
    sensors:
      dark_outside:   ### Set the sensor to true if luminance is below 50 at exerior stairs
        entity_id: sensor.pir_utetrapp_luminance
        friendly_name: Mørkt utendørs
        value_template: >-
          {{ states('sensor.pir_utetrapp_luminance')|float < 50 }}
        icon_template: >-
          {% if is_state('binary_sensor.dark_outside', 'on') %}
            mdi:weather-night
          {% else %}
            mdi:weather-sunny
          {% endif %}
      dark_inside:   ### Set the sensor to true if luminance is below 100 at exerior stairs
        entity_id: sensor.pir_utetrapp_luminance
        friendly_name: Mørkt innendørs
        value_template: >-
          {{ states('sensor.pir_utetrapp_luminance')|float < 150 }}
        icon_template: >-
          {% if is_state('binary_sensor.dark_inside', 'on') %}
            mdi:weather-night
          {% else %}
            mdi:weather-sunny
          {% endif %}

There you go!
It seems to stop at 55
But still I need this to turn the exterior lights on when the light is around 50 :slight_smile:

If it stops at 55 then that explains why the Template Binary Sensor never reports on (because it uses a threshold of 50).

Change the threshold value to 60.

I will definitely play with that value.
Fun fact: We are soon to experience sunsets as late as 22:30, which will make this even tougher.

in your case, a look at the binary_sensor threshold might be useful. you can set an upper and lower threshold, and a window to that threshold.
minimum config in your case

binary_sensor:
  - platform: threshold
    entity_id: sensor.pir_utetrapp_luminance
    lower: 50

I use these for my appliances. they’re always on, so on/off is not of interest, but the power consumption checks to see if they are at work:


very nice and very trustworthy (of course, only if the base sensor is)

Did increasing the threshold to 60 fix the problem?

I am not sure if that made the trick, or if the main problem was the sensor was not reporting correctly the night before. Last night the luminance dropped to 36 lux, first time at 21:19.

As you can see the sensor.pir_utetrapp_luminance (Utetrapp) sensor set the binary_sensor.dark_outside correctly last night, as opposed to the night before (earlier post/screen dump).

I guess the scripts and automations are working now.
Thank you for your help on this guys :grinning: :+1:t2: