Problem with scan interval

Hi,
i have a problem with my component. Here is the layout of my circuit, the yaml (configuration) and the sensory.py. I would like to query/pool these buttons (sensor.py) and I need to get under 500 ms as I can modify my code to have an optimal result. Thanks

Circuit Scheme
image

Buttons.yaml

#################################################################
## BUTTONS
#################################################################
- platform: atena
  host: 192.168.2.172
  port: 9761
  address: 86
  U: d1
  name: Camera 1P Button
  type: light
  scan_interval:
    milliseconds: 500

- platform: atena
  host: 192.168.2.172
  port: 9761
  address: 90
  U: d1
  name: Locale Tecnico Button
  type: light
  scan_interval:
    milliseconds: 500

sensor.py

"""Platform for light integration."""
import logging

import voluptuous as vol
from datetime import timedelta
from . import atenapy
#import asyncio
import homeassistant.helpers.config_validation as cv
# Import the device class from the component that you want to support
from homeassistant.components.binary_sensor import BinarySensorDevice, PLATFORM_SCHEMA

from homeassistant.helpers.entity import Entity

from homeassistant.const import (
    DEVICE_DEFAULT_NAME, 
    CONF_HOST, 
    CONF_NAME, 
    STATE_ON, 
    STATE_UNAVAILABLE,
    #CONF_SCAN_INTERVAL,
)


_LOGGER = logging.getLogger(__name__)

_LOGGER.debug('message')


DEFAULT_NAME = "name"
CONF_HOST = "host"
CONF_PORT = "port"
CONF_ADDRESS = "address"
CONF_U = "U"
CONF_TYPE = "type"
#SCAN_INTERVAL = timedelta (milliseconds = 10)


# Validation of the user's configuration

    
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_HOST): cv.string,
    vol.Required(CONF_PORT): cv.positive_int,
    vol.Required(CONF_ADDRESS): cv.positive_int,
    vol.Required(CONF_U): cv.string,
    vol.Optional(DEFAULT_NAME): cv.string,
    vol.Optional(CONF_TYPE): cv.string,
    #vol.Optional(CONF_SCAN_INTERVAL): cv.time_period,
})



def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Remote ATENA PE switch."""

    # Assign configuration variables.
    # The configuration check takes care they are present.
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    address = config.get(CONF_ADDRESS)
    U = config.get(CONF_U)
    name = config.get(DEFAULT_NAME)
    device_class = config.get(CONF_TYPE)

    add_entities([AtenaPEBinarySensor(host, port, address, U, name, device_class)])


class AtenaPEBinarySensor(BinarySensorDevice):
    """Representation of an Atena Switch."""

    def __init__(self, host, port, address, U, name, device_class):
        """Initialize the Atena PE switch device."""
        self._host = host
        self._port = port
        self._address = address
        self._U = U
        self._name = name
        self._device_class = device_class
        self._state = 'off'



    @property
    def name(self):
        """Return the display name of this sensor."""
        return self._name

    @property
    def device_class(self):
        """Return the display name of this sensor."""
        return self._device_class

    @property
    def state(self):
        """Return true if device is on."""
        return self._state 


    def update(self):
        """Fetch new state data for the sensor.
        
        This is the only method that should fetch new data for Home Assistant.
        """
        
        #SLEEP_DURATION = 1e-3  # 5 ms sleep
        #_LOGGER.debug(SLEEP_DURATION)
        #while True:
        #    await asyncio.sleep(SLEEP_DURATION)
        info_state = atenapy.atena_read_switch_status(self._host, self._port, self._address, self._U)
        
        info_state_sensor = []

        if info_state == True:
            info_state_sensor = 'on'
        else:
            info_state_sensor = 'off'
        
        _LOGGER.debug(name,info_state)
        self._state = info_state_sensor


You can never guarantee under 1 second on HA as that is how often it updates it’s input and output ‘registers’
So a time delay in a system of 1500ms will take ‘at least’ 2 seconds.
If you need less you will need to look at a more hardwired solution

what you mean by hardwired solution. my system is not wifi actuators (PE) are connected with lan cable

As in a wire comes from a switch and goes to a bulb
You know - Faraday level switching.(Speed of light stuff)
What is the application, why do you need such fast response ?
HA is meant for home automation not controlling industrial multi-variable systems or nuclear reactors.
Normally having a light come on in one location to respond to another light being switched off in another location with a 2 second gap is not a problem, if it is, you’re using the wrong switch arrangement.

Then my case is the following:

like the scheme shown above I have a device that gives me the status only if called, on this device I connected a button, the button continuously gives me the vaolre 1 only when I press changes the value to 0 and then immediately returns to 1. How do I intercept this state with a home assistant if I poll for a second?

I would also be willing to pay for a solution that works on my device and create an ad hoc component … but I don’t know anyone so experienced as to deal with the problem.

Your english is excellent but I can tell from the flow that your first language is something else.
Let me see if I can paraphrase what you are saying.
You have a button, that ony gives a 0 output when pressed, otherwise gives 1
And you are concerned that you will miss the button press ?
It sounds like you need a ‘delay off relay’.
So a button energises the relay, it stays on for … (say) 1.5 secs and this makes the contact for your device.
Depends what supply you have locally.
Another means would be to change the button to a switch and then operate on a leading or trailing edge (i.e. change of state). You can get latching buttons that would do this.
This does not seem right to me, why have a system that can’t pick up on it’s own switches ?
Normally s switch connected to HA will report the change, even if it happened 950ms earlier than the register gets updated. The device caches the change till HA receives it.
Is that close to your needs, or is there something else we’ve missed ?

Exactly, you understood very well, my system comes from an old home automation system 25 years ago, I wouldn’t want to change all the hardware because it’s fine, the problem that can’t progress in any way. The company that installed the system gives me also the communication protocol of their devices. I managed to build a custom component for HA and I can turn lights on and off and raise and lower blinds, my problem is the states. The “delay off relay” option is not possible for a number of reasons that are not cheap. I do not recommend changing buttons because it is not the optimal solution. So I have to find a software solution that simulates the correct functioning of the “switch”. In other words, this function totally independent from HA must poll (in less than 1 sec) on a list of devices (they would be my buttons) and expose the states to a register where homeassistant can access and read. It is easy to say but you can do it much more complicated.

You know someone who can help me?

Well, we can bump this thread occasionally to to keep it near the top of the list, hoping someone can help
But I think your constraints prevent an HA solution.
I hope I’m wrong
Good luck
Mutt

ok thanks.

Hi I have a similar configuration, how can I add - platform: atena?
Is it a custom_component of yours?

I have similar situation, Modbus RTU device with digital inputs. Buttons are connected to that inputs. So, how I can handle ‘button click’ event if it is around 100-300ms?

Hi there,
I’ve a similar situation too, so I give this thread my bump :slight_smile: . This is my hardware:

  • one modbus over tcp\ip module with 32 digital inputs and some normally open momentary buttons
  • one modbus over tcp\ip module with 32 digital outputs, some relays for controlling lights
    Then there’s an automation that commutates the n-th light on the n-th button press.
    When a button is pressed “very shortly” the system misses the event and doesn’t trigger the automation, event if the DI module is able to pick it up. Also the same flow is working on node-red (cause I can set a 1ms pull time).

As Mutt suggested, changing the momentary buttons with switches (and updating the automation accordly) is working a solution, but in my case 2 switches take the space of 4 buttons (typical electrical box with 2 button per module vs 1 rocking switch per module)

Can the 1s be modded in some config file?

Has anyone found a better approach for having an automation triggered by a button press?

Cheers!

I have the exact same problem, an hardwired remote control that sometimes misses the button press because the 1sec polling is not enough for button detection.
It would be nice if that 1sec resolution could be lowered in some way.

I have same situation… Did you find a solution?
I was thinking since nodered can get the signals (even when less then 100 ms) nodered can pass it through to home assistant?
I did not try this yet but that is how I planned on doing this… Not sure if it will work.

If you found an other solution, I would like to hear it!

(I use a wago plc, but I only want to use the plc as IO module…)

I’ve tested node red: it’s easy to work with but I’d like to avoid adding another piece of software to the chain just for one feature.

At the moment I’ve built a very minimal python script: lights-automation/main.py at 1809c088fa135e9290f64f9b619da7e032a9c0d5 · setola/lights-automation · GitHub wich is running in a container…

At the moment I can read the input status very quickly (100ms scan time is safe, 10 is a bit unstable, but it’s not required in my scenario).

My plan is to scan the inputs and trigger an event on HA, but atm I’m missing the communication with HA…
It’s planned, but I’ve no idea on when I can develop something else on it…