Automation if more then X lights are on, send notification

Thanks.

I wish to use Zanzito, and without opening HA query a mqtt topic about the number of lights on at any time.

So u should publish the sensor counter light number on mqtt topic, not sure how to do that

you can use the mqtt.publish service in an automation or a script. this is an example how i trigger a mqtt.publish in an automation:

automation:
  alias: KĂźche Push Button 3
  trigger:
    - platform: mqtt
      topic: 'kueche1/button3/Control'
      payload: '1'
  action:
    - service: mqtt.publish
      data:
        topic: "gong/Control"
        payload: "7"

in your case you’ll need to use a template:

    […]
    - service: mqtt.publish
      data:
        topic: "whatever/xxx"
        payload_template: {{states.sensor.counter_lights_on.state}}

is it still an automation, without a trigger?

the service call is just the action which you can use in an automation or a script sequence.

I did this but have an errror

in script:

  count_lights_on:
    - service: mqtt.publish
      data:
        topic: "sensor/count_lights_and_switches_on"
        payload_template: {{states.sensor.counter_lights_on.state}}

2017-06-27 17:25:33 ERROR (Thread-1) [homeassistant.util.yaml] invalid key: “OrderedDict([(‘states.sensor.counter_lights_on.state’, None)])”
in “/home/homeassistant/.homeassistant/script.yaml”, line 323, column 0
2017-06-27 17:25:33 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: invalid key: “OrderedDict([(‘states.sensor.counter_lights_on.state’, None)])”
in “/home/homeassistant/.homeassistant/script.yaml”, line 323, column 0

you probably need to put the template in quotes:

    payload_template: "{{states.sensor.counter_lights_on.state}}"

yes now no errors.

But the topic does not get its value (the automation works fine)

bt how do I make it run all the time?

I do not understand how to publish the number of lights ON on mqtt (and that it changes whenever a lights goes ON or OFF)

This solution does not work anymore, error: “entity_id has been deprecated”.

What’s the proper way now?

You no longer need the entity config. Just remove it and it will work fine.

the deprecation notice (not an error btw., just a warning) has been removed in HA 0.63.x. if you upgrade, you won’t be bothered by it anymore and can leave the entity_id in place. leaving it out might not work, since the auto entity_id detection does not work in cases like this, where the entitys are not mentioned explicitly.

see here:

https://github.com/home-assistant/home-assistant/pull/12234

Ok, I updated to 0.63 but the sensor shows no value. This is my config entry:

  - platform: template
    sensors:
      pir_sensors_on:
        unit_of_measurement: 'Sensors'
        friendly_name: 'Sensors ON'
        entity_id:
          - binary_sensor.fibaro_motion_w1_sensor
          - binary_sensor.fibaro_motion_w2_sensor
          - binary_sensor.fibaro_motion_f_sensor
          - binary_sensor.fibaro_motion_b_sensor
          - binary_sensor.fibaro_motion_s1_sensor
          - binary_sensor.fibaro_motion_s2_sensor
        value_template: >-
          {%- for item in states.group.Bewegungssensoren.attributes.entity_id  if states(item) == "on" %}
            {% if loop.last %}
              {{loop.index}}
            {% endif %}
          {% endfor %}

What’s wrong?

1 Like

try the template debugger and paste the template in there; what does it say?

All of a sudden it works, I can not tell why.

Thanks Diplix!

If no sensor is in state ‘ON’ there will be no output. What may I do to have a zero then?

{% if states('group.inside') == 'on' %}
{% for e in states.group.inside.attributes.entity_id if states(e) == 'on'  %}
{% if loop.last %}
{{ loop.index }}
{% endif %}
{%- endfor -%}
{% else %}
0
{% endif %}
2 Likes

Thanks Petro, solved!

1 Like