Automation doesn't trigger

I’m trying to make my receiver turn down the volume automatically when it gets too loud.
I have added a microphone to a ESP8266 and use the ADC to get the “volume”. The return is a voltage reading.

ESPhome:

sensor:
  - platform: adc
    pin: A0
    name: "Sound"
    update_interval: 0.2s
    filters:
      - multiply: 3.3

The value is visible in Home Assistant as it should.
image
I also added a stats sensor to even out the spikes some.

Configuration.yaml

sensor:
  - platform: statistics
    entity_id: sensor.sound
    sampling_size: 5

This means the original value, sensor.sound, updates every 0.2 seconds and sensor.stats is average of 5 values (1 second).

The automation is:

- id: '1586672460751'
  alias: skruva ner förstärkaren
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states.sensor.sound.state > 1.9 }}'
  condition: []
  action:
  - data: {}
    service: script.1586684255359 # script that turns down the volume of the receiver

Why is that not triggered as it should? Not even with {{ states.sensor.stats.state > 1.9 }} does it work.
Anyone that spots the problem?

Why are you using a template trigger when a numeric state trigger will do?

- id: '1586672460751'
  alias: skruva ner förstärkaren
  trigger:
    platform: numeric_state
    entity_id: sensor.sound.state
    above: 1.9
  action:
    service: script.1586684255359

I actually tried numeric too but I couldn’t get that working either.
I can give it a try again, I was using the GUI when I made the numeric, maybe it works better in “notepad”.
I’ll try it in an hour or so.

How would I do that? Will that not require a new flash of the board to change the threshold?
I’m planing on having the value in the front to be able to change it.

Also I would be doing all the filtering on the ESP so that you are not spamming home assistant with updates.

sensor:
  - platform: adc
    pin: A0
    name: "Sound"
    update_interval: 0.2s
    filters:
      - multiply: 3.3
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
          send_first_at: 10

Actually that probably won’t work as it is an AC signal the average will be 0. You will have to remove the negative values with a lambda abs() function first.

2 Likes

I’m not sure there are any negative values.
I thought so too and was prepared to build a ac-dc circuit but it seems ESPhome does that.

Anyways, if I send every 10 value that means it takes two seconds between each value home assistant gets.
That means it takes at least two seconds before home assistant sends the command to lower the volume.

The whole idea is to get remove all the stupid spikes in TV broadcasts have.
Like the intro to a TV-show, or any other spikes that you have to reach for the remote.

Sure an action move will not be suitable for this but every now and then the editors of a movie/TV-show thinks it’s appropriate to raise the volume so that the windows start shaking here.
I don’t agree with them… And not my sleeping kids either.

You’re right. The audio seems to be biased about 1/2 Vcc (1.8V) so the average will be 1.8V. You would have to subtract 1.8 to bias it about 0 then use ABS() to convert all the negative values to positive.

So send every 5 samples (= 1 second). Still way better than 5 times a second.

Even better still would be to use a template in the ESP to only send the ‘too loud’ signal when required.

You are correct with vcc/2.
But that is 3.3/2 = 1.65 and that is also what my multimeter shows if I have it on DC.
Looking at the graphs the lowest values are at about 1.65-ish. I believe there can be some error values too.

Pardon my ignorance, what is the real problem with pushing values to HA that often?
As long as it’s still responsive and react to things (emh… Could that be the problem? That the spikes wasn’t captured in the automation because it was busy with new values coming in).

Can esphome run HA scripts?
That could mean it can do all the processing and only bother HA when the script needs to run.

Is that possible?
I will look in to that!

No but it has a simpler version.

Definitely. See: Template Binary Sensor — ESPHome

Not sure I understand how that works in my case.
But I found another method that may work(?).
Add a switch that is on or off depending on the value.


switch:
  - platform: template
    name: "snd"
    id: snd
    
sensor:
  - platform: adc
    pin: A0
    name: "Sound"
    update_interval: 0.2s
    filters:
      - multiply: 3.3
    on_value_range:
      - above: 0.57
        then:
          - switch.turn_on: snd
      - below: 0.53
        then:
          - switch.turn_off: snd

I assume the values are supposed to be before the multiply by 3.3 at least it seems that way.
In the console I see the below.

Note the two lines indented with *.

[14:02:21][D][adc:056]: 'Sound': Got voltage=0.00V
[14:02:21][D][sensor:092]: 'Sound': Sending state 0.00000 V with 2 decimals of accuracy
*****[14:02:21][D][switch:025]: 'snd' Turning OFF.
[14:02:21][D][adc:056]: 'Sound': Got voltage=0.50V
[14:02:21][D][sensor:092]: 'Sound': Sending state 1.64033 V with 2 decimals of accuracy
*****[14:02:21][D][switch:021]: 'snd' Turning ON.
[14:02:21][D][adc:056]: 'Sound': Got voltage=0.63V
[14:02:21][D][sensor:092]: 'Sound': Sending state 2.07217 V with 2 decimals of accuracy

But in Home Assistant Switch.snd does not change.

Tried to change the automation in notepad and reload the automations but the automation just disappeared from HA…
As far as I can see it’s correct.

- id: '1586672460751'
  alias: skruva ner förstärkaren
  description: ''
  trigger:
    platform: numeric_state
    entity_id: sensor.sound.state
    above: 1.9
  condition: []
  action:
  - data: {}
    service: script.1586684255359

Actually I can’t even get the switch to turn on at all…

I removed the switch off thinking that I could do that in the automation. But that didn’t help.
Then I tried to call the service to turn on or toggle switch.snd but it doesn’t work.

I don’t know much about esphome so feel free to discount the following.
I know that you can set esphome devices to perform all sorts of subsecond tasks. But HA only reads on a 1 sec interval and only writes on a one second interval.
So if you are sending 0.2s updates, it probably only reads the last one.
If you are lucky you might get the write response in the same cycle (plc’s always do read, process write, I’m guessing HA does the same) BUT you may find yourself in a situation where your sensor read the high value 0.8 secs ago, processes it then it misses the cycle (postulating worst case here) and posts 1 second later. (1.8 sec response)
It’s still a damn sight quicker than : -
Ouch, that’s loud !
Where did I put the remote ?
Ah, over there OR under that cushion
Get it
Oh it’s upside down
Which button is it again ?

Again not sure if possible but if you could get the esphome device to send the volume command ? (probably not)
Sorry to have butted in.

Could be!

But I got it working in a different way. Will post tomorrow how.
Basically ESPhome sets a pin high on the board, this is seen by HA that runs the script and sets the pin low again.
It works. Just need some adjustments.

Now I just need to make the board stop sending the sound value. There is no need for that value in HA anymore.

1 Like